删除 matplotlib 中 3d 图周围的白色区域

问题描述 投票:0回答:1

我使用 matplotlib 创建了高斯曲面图:

num_pts = 1000
σ = 1.5

x = np.linspace(-5, 5, num_pts)
kernel1d = np.exp(-np.square(x) / (2 * σ * σ))
kernel2d = np.outer(kernel1d, kernel1d)

X, Y = np.meshgrid(x, x)

fig, ax = plt.subplots(figsize=(6,6), subplot_kw={"projection":"3d"})
ax.plot_surface(X, Y, kernel2d, cmap="viridis", linewidth=0, antialiased=True)
ax.set(xticks=[], yticks=[], zticks=[])
ax.grid(False)
ax.axis('off')

fig.savefig("test.svg", format="svg", transparent=True)

输出在实际绘图周围有很多死区。

我尝试了

layout=tight
但没有任何效果。

假设不需要任何轴:刻度、标签、网格等,如何最大化图形中的表面尺寸...

python matplotlib plot figure
1个回答
0
投票

在定义绘图时,设置轴的限制将在功能上放大和缩小。

layout=tight
会影响图周围的空白,或者在子图的情况下会有效地将它们压缩得更近。

# change the limits of the axes to zoom in
ax.set_xlim(-2, 2)
ax.set_ylim(-2, 2)
ax.set_zlim(0, 1)

结果:

© www.soinside.com 2019 - 2024. All rights reserved.