我想从 3D 场景中删除边框,如下所述。知道该怎么做吗?
这里是生成当前场景的代码:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# Create figure
plt.style.use('dark_background') # Dark theme
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# Make panes transparent
ax.xaxis.pane.fill = False # Left pane
ax.yaxis.pane.fill = False # Right pane
# Remove grid lines
ax.grid(False)
# Remove tick labels
ax.set_xticklabels([])
ax.set_yticklabels([])
ax.set_zticklabels([])
# Print chart
file_path = 'charts/3d.png'
fig.savefig(file_path, bbox_inches='tight', pad_inches=0.05, transparent=True)
我通常将书脊和窗格的 Alpha 通道设置为 0,最后删除刻度线:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# Create figure
plt.style.use('dark_background') # Dark theme
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# Make panes transparent
ax.xaxis.pane.fill = False # Left pane
ax.yaxis.pane.fill = False # Right pane
# Remove grid lines
ax.grid(False)
# Remove tick labels
ax.set_xticklabels([])
ax.set_yticklabels([])
ax.set_zticklabels([])
# Transparent spines
ax.w_xaxis.line.set_color((1.0, 1.0, 1.0, 0.0))
ax.w_yaxis.line.set_color((1.0, 1.0, 1.0, 0.0))
ax.w_zaxis.line.set_color((1.0, 1.0, 1.0, 0.0))
# Transparent panes
ax.w_xaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
ax.w_yaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
# No ticks
ax.set_xticks([])
ax.set_yticks([])
ax.set_zticks([])
创建图形时尝试添加以下参数
plt.figure(frameon=False)
编辑: 包含注释的行是我添加/更改的
import matplotlib.pyplot as plt
import numpy as np # Imported numpy for random data
from mpl_toolkits.mplot3d import Axes3D
plt.style.use('dark_background')
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.xaxis.pane.fill = False
ax.yaxis.pane.fill = False
ax.set_xticklabels([])
ax.set_yticklabels([])
ax.set_zticklabels([])
# Remove the axis
plt.axis('off')
# Random data to illustrate
zdata = 15 * np.random.random(100)
xdata = np.sin(zdata) + 0.1 * np.random.randn(100)
ydata = np.cos(zdata) + 0.1 * np.random.randn(100)
ax.scatter3D(xdata, ydata, zdata, c=zdata, cmap='Greens')
# Print chart (savefig overwrites some styling configs, better this way)
with open('3d.png', 'wb') as outfile:
fig.canvas.print_png(outfile)
删除它们的一种简单方法是使用
set_axis_off()
。import numpy as np
from matplotlib import cm
import matplotlib.pyplot as plt
σ = 3
vG = np.linspace(-10, 10, 201)
mX, mY = np.meshgrid(vG, vG)
# Delta as a Gaussian with σ ➔ 0
mδ = np.exp(-np.square(mX) / (0.1 * σ * σ)) * np.exp(-np.square(mY) / (0.1 * σ * σ))
mG = np.exp(-np.square(mX) / (2 * σ * σ)) * np.exp(-np.square(mY) / (2 * σ * σ))
hF, vHa = plt.subplots(nrows = 1, ncols = 3, figsize = (13, 4), subplot_kw={"projection": "3d"})
vHa = vHa.flat
vHa[0].plot_surface(mX, mY, mδ, rstride = 1, cstride = 1, cmap = cm.coolwarm, linewidth = 0, antialiased = False)
vHa[0].set(xticks = [], yticks = [], zticks = [], title = r'$\delta \left[ i, j \right]$')
vHa[0].set_axis_off()
vHa[1].plot_surface(mX, mY, mG, rstride = 1, cstride = 1, cmap = cm.coolwarm, linewidth = 0, antialiased = False)
vHa[1].set(xticks = [], yticks = [], zticks = [], title = r'$g \left[ i, j \right]$ (Gaussian Kernel)')
vHa[1].set_axis_off()
vHa[2].plot_surface(mX, mY, mδ - mG, rstride = 1, cstride = 1, cmap = cm.coolwarm, linewidth = 0, antialiased = False)
vHa[2].set(xticks = [], yticks = [], zticks = [], title = r'$h \left[ i, j \right]$')
vHa[2].set_axis_off();