pythonmatplotlibsubplot

How to increase the space between the subplots and the figure?


I'm using a python code to plot 3D surface. However, the z-axis label get cut by the figure. Here is the code :

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure(figsize=(12, 10), facecolor='lightblue')
x = np.linspace(0, 10)
y = np.linspace(0, 10)
X, Y = np.meshgrid(x, y)

for idx in range(4):
    Z = np.cos(X) - np.sin(np.pi*idx/4 * Y)
    ax3D =  fig.add_subplot(2, 2, idx+1, projection='3d')
    ax3D.plot_surface(X, Y, Z, cmap="viridis")
    ax3D.set_zlabel("Title")

plt.show()

The result : 3D surface plots

Is it a possible to include the axis label in the axe ? Or to increase the space subplots and the figure ?

I have tried different options such as : plt.subplots_adjust(left=0, bottom=0, right=0.8, top=0.7, wspace=0.5, hspace=0.2) or fig.tight_layout(); but nothing seems to resolve my problem.


Solution

  • One solution is to zoom out to decrease the size of each subplot (set_box_aspect). One can also play with the three angles that defines the view: elevation, azimuth, and roll (view_init).

    fig = plt.figure(figsize=(12/2, 10/2), facecolor='lightblue')
    x = np.linspace(0, 10)
    y = np.linspace(0, 10)
    X, Y = np.meshgrid(x, y)
    
    for idx in range(4):
        Z = np.cos(X) - np.sin(np.pi*idx/4 * Y)
        ax3D =  fig.add_subplot(2, 2, idx+1, projection='3d')
        ax3D.view_init(elev=30, azim=70, roll=0)  
        ax3D.set_box_aspect(aspect=(1,1,1), zoom=0.8)
        ax3D.plot_surface(X, Y, Z, cmap="viridis")
        ax3D.set_zlabel("Title")
    fig.tight_layout()
    plt.show()
    

    plot