使用matplotlib并排绘制2个3d表面

时间:2018-02-20 11:28:09

标签: python matplotlib 3d

我试图扩展this example并绘制2个三维表面图。

这是代码,它可以工作,但由于某些原因,显示裁剪和挤压的图。如何解决?

from tqdm import tqdm

enter image description here

更新

设置#Plot side by side def plot_xyz_data(): n_radii = 8 n_angles = 36 radii = np.linspace(0.125, 1.0, n_radii) angles = np.linspace(0, 2*np.pi, n_angles, endpoint=False) angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1) x = np.append(0, (radii*np.cos(angles)).flatten()) y = np.append(0, (radii*np.sin(angles)).flatten()) z1 = np.sin(-x*y) z2 = np.cos(x**2) print('x.shape', x.shape) print('y.shape', y.shape) print('z1.shape', z1.shape) print('z2.shape', z2.shape) fig = plt.figure() ax1 = fig.add_subplot(121,projection='3d') surf1 = ax1.plot_trisurf(x, y, z1, cmap=cm.jet, antialiased=True) ax1.set_xlabel('x label') ax1.set_ylabel('y label') ax1.set_zlabel('z1 label') ax2 = fig.add_subplot(122,projection='3d') surf2 = ax2.plot_trisurf(x, y, z2, cmap=cm.jet, antialiased=True) ax2.set_xlabel('x label') ax2.set_ylabel('y label') ax2.set_zlabel('z2 label') plt.savefig("sample.png",bbox_inches='tight',dpi=100) plt.show() plot_xyz_data() 看起来有帮助,但仍然不完美:' y label'与数字重叠。有什么办法解决吗? enter image description here

1 个答案:

答案 0 :(得分:1)

plt.tight_layout()

之前添加plt.savefig()