mrgloom
mrgloom

Reputation: 21692

Plot 2 3d surface side by side using matplotlib

I'm trying to extens this example and plot 2 3d surface plots side by side.

Here is the code, it works, but for some reason plots displayed cropped and squeezed. How to fix it?

#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()

enter image description here

Update:

Looks like setting fig = plt.figure(figsize=(16, 12)) helps, but still not perfect: 'y label' overlaps with numbers. Any way to fix it? enter image description here

Upvotes: 0

Views: 4026

Answers (1)

Joe
Joe

Reputation: 7161

Add plt.tight_layout() before plt.savefig()

Upvotes: 1

Related Questions