删除3D打印matplotlib的空白

2024-10-06 11:18:22 发布

您现在位置:Python中文网/ 问答频道 /正文

为了解决一个很简单的问题,我已经在谷歌上搜索了好几天: 我想用matplotlib生成一个三维绘图。这很好用,我可以在正确的位置旋转绘图。在

fig = plt.figure(figsize=(13,6), facecolor="white")

ax1 = plt.subplot2grid((3,3), (0,0), colspan=2, rowspan=2, projection="3d")



def cc(SpectrumType):
    if SpectrumType == "Normal":
        return "tab:gray"
    if SpectrumType == "Anomaly":
        return mcolors.to_rgba("r", alpha=1)   


# X Axis : equally space from 0 to 10
xs = np.arange(0, 20, 0.4)

verts = []

# 3 D axis: draw 4 plots at points 0, 1, 2, 3
zs = [0.0, 2.0, 4.0, 6.0, 8.0, 10.0, 12.0, 14.0, 16.0, 18.0]

afactor = 1
for z in zs:
    afactor = 1
    if z==10.0:
        afactor = 5
    ys = afactor*np.random.rand(len(xs))
    ys[0], ys[-1] = 0, 0
    verts.append(list(zip(xs, ys)))



poly = PolyCollection(verts)
poly.set_alpha(0.5)

ax1.add_collection3d(poly, zs=zs, zdir='x')

ax1.set_xlabel('X')
ax1.set_xlim3d(-1, 21)
ax1.set_ylabel('Y')
ax1.set_ylim3d(0, 20)
ax1.set_zlabel('Z')
ax1.set_zlim3d(0, 5)
ax1.set_title("Plot 1")

ax1.view_init(8,-82) 
ax1.get_proj = lambda: np.dot(Axes3D.get_proj(ax1), np.diag([0.3, 1, 0.3, 
0.3]))


plt.tight_layout(pad=0)#, w_pad=0.5, h_pad=5.0)
fig.subplots_adjust(top=1, bottom=0, left=0, right=1)
fig = plt.gcf()
plt.legend(loc='upper right')#, bbox_to_anchor=(0.5, 0.5))
plt.show()

生成的3D打印:

enter image description here

问题:图的左侧总是有空白。这让我发疯了!我想把这个3D作为一个子图添加到网格中,但是这个空白使整个图形看起来很难看。请帮忙!如何删除左边的空白?在

非常感谢!在


Tags: toifnpfigpltsetxspad