Matplotlib 3d plot: how to get rid of the excessive white space?

前端 未结 1 2054
春和景丽
春和景丽 2021-01-22 14:04

If I make a 3d plot in Matplotlib:

from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.gca(projection=\'3d\')

x_labels = [10,20,30]
x = [1,2,3,4         


        
相关标签:
1条回答
  • 2021-01-22 14:16

    It's probably too late, but I came across similar problems and here is what I did to remove the white space: use fig.subplot_adjust() to put left/right outside the normal region. In your case I found fig.subplot_adjust(left=-0.11) gives a reasonable result.

    Full code below:

    from mpl_toolkits.mplot3d import Axes3D
    fig = plt.figure()  
    ax = fig.gca(projection='3d')
    
    x_labels = [10,20,30]
    x = [1,2,3,4]
    y = [3,1,5,1]
    legend = False
    
    for label in x_labels:
        x_3d = label*np.ones_like(x)
        ax.plot(x_3d, x, y, color='black', label='GMM')
        if legend == False:
            ax.legend()
            legend = True
    
    ax.set_zlabel('test')
    
    fig.tight_layout()
    fig.subplots_adjust(left=-0.11)  # plot outside the normal area
    

    0 讨论(0)
提交回复
热议问题