我已经在谷歌上搜索了几天,以解决一个容易出错的问题: 我想用matplotlib生成一个3D图。这很好用,我可以将图形旋转到正确的位置。
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图:
问题:图的左侧总是有空白区域。这让我抓狂!我想将这个3D作为子图添加到网格中,但是这个白色空间使得整个图形看起来非常难看。请帮忙 !如何删除左侧的空白区域?
非常感谢!