user1084135
user1084135

Reputation: 43

How to rescale a plot in a subplot with matplotlib

I have 4 subplots with a different 3D plot with a colorbar. I want to plot a XY view of my 3D plot, remove the x,y,z axis and resize my plot to use all the space available in the subplot such that the XY view has the same height as the colorbar. I can remove the axis but I do not know how to resize the image. I attached a working code to illustrate this.

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import matplotlib.tri as mtri
import matplotlib 
import numpy as np

# Create 3D function
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)[..., np.newaxis]
x = np.append(0, (radii*np.cos(angles)).flatten())
y = np.append(0, (radii*np.sin(angles)).flatten())
z = np.sin(-x*y)

fig = plt.figure()
for ii in range(1, 4):
    #Plot
    # ============================================================================ 
    ax = fig.add_subplot(2,2, ii, projection='3d')
    cs =ax.plot_trisurf(x, y, z, linewidth=0.2, antialiased=True)

    ax.view_init(90, 0)
    plt.title(ii)
    # ax.axis('off')
    plt.grid(b=None)

    # Create color bar
    # ============================================================================ 
    norm = matplotlib.colors.Normalize(vmin = 0, vmax = 1, clip = False)
    m = plt.cm.ScalarMappable(norm=norm)
    m.set_array([])
    plt.colorbar(m)

plt.tight_layout()
plt.show()
#plt.savefig("test.pdf",bbox_inches='tight')

Any idea how can I do this?

Upvotes: 1

Views: 509

Answers (1)

finefoot
finefoot

Reputation: 11302

I have added

plt.gca().set_axis_off()
plt.axis([0.6 * x for x in plt.axis()])

to your code which hides the axes and sets the view to 60% of its previous value. The result looks like this:

Figure 1

Full code:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import matplotlib.tri as mtri
import matplotlib 
import numpy as np

# Create 3D function
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)[..., np.newaxis]
x = np.append(0, (radii*np.cos(angles)).flatten())
y = np.append(0, (radii*np.sin(angles)).flatten())
z = np.sin(-x*y)

fig = plt.figure()
for ii in range(1, 4):
    #Plot
    # ============================================================================ 
    ax = fig.add_subplot(2,2, ii, projection='3d')
    cs =ax.plot_trisurf(x, y, z, linewidth=0.2, antialiased=True)

    ax.view_init(90, 0)
    plt.title(ii)
    # ax.axis('off')
    plt.grid(b=None)

    # Create color bar
    # ============================================================================ 
    norm = matplotlib.colors.Normalize(vmin = 0, vmax = 1, clip = False)
    m = plt.cm.ScalarMappable(norm=norm)
    m.set_array([])
    plt.colorbar(m)

    plt.gca().set_axis_off()
    plt.axis([0.6 * x for x in plt.axis()])

plt.tight_layout()
plt.show()
#plt.savefig("test.pdf",bbox_inches='tight')

Upvotes: 1

Related Questions