Search code examples
pythonmatplotlibsubplotmatplotlib-3d

Set size of figure with 3d subplots


I have a figure where I want to plot a 3d scatter and a normal plot in 2 subplots. I use this code

fig = plt.figure(1)
ax = fig.add_subplot(211, projection='3d')
ax2 = fig.add_subplot(212)

then I use ax and ax2 to plot my data in the right place. But The figure I get is too small, how can I make it bigger?, adding figsize=(w, h) to the first line doesn't have any effect and results in an error if I add it to the second or third line.:

AttributeError: 'Axes3DSubplot' object has no attribute 'set_figsize'


Solution

  • Adding figsize=(w,h) to the first line should do the trick. What do you mean by "it has no effect"?

    For example:

    import matplotlib.pyplot as plt
    from mpl_toolkits.mplot3d import Axes3D
    
    fig1=plt.figure(figsize=(8,5))
    ax11=fig1.add_subplot(211,projection='3d')
    ax12=fig1.add_subplot(212)
    
    fig1.get_size_inches()
    # array([ 8.,  5.])
    
    fig2=plt.figure(figsize=(4,4))
    ax21=fig1.add_subplot(211,projection='3d')
    ax22=fig1.add_subplot(212)
    
    fig2.get_size_inches()
    # array([ 4.,  4.])
    
    fig1.savefig('fig1.png',dpi=300)
    fig2.savefig('fig2.png',dpi=300)
    

    After saving figures as png's at 300dpi:

    $ identify fig1.png
    >>> fig1.png PNG 2400x1500 2400x1500+0+0 8-bit sRGB 118KB 0.000u 0:00.000
    
    $ identify fig2.png
    >>> fig2.png PNG 1200x1200 1200x1200+0+0 8-bit sRGB 86.9KB 0.000u 0:00.000