savefig() saves different aspect ratio than show()

The following script (using matplotlib 1.3.1) should create a 1-row, 3 column subplot with only one panel. The figure is displayed with show (which looks as desired) and also saved as a pdf using savefig. However, savefig has squeezed the wide figure into the normal aspect ratio, which it should not do.

#!/usr/bin/env python
import matplotlib
matplotlib.use('Qt4Agg')
from matplotlib.pyplot import *
from numpy import *

figure()
mgr = get_current_fig_manager()
mgr.window.setGeometry(0,0,800,200)
subplot(1,3,1)
x=linspace(0,2*pi,100)
plot(x,sin(x))
savefig('test.pdf')
show()

Author: Fantashit

1 thought on “savefig() saves different aspect ratio than show()

  1. I also came across this issue, but for a much simpler case.

    import numpy as np
    import matplotlib.pyplot as plt
    from mpl_toolkits.mplot3d import Axes3D
    
    fig = plt.figure(figsize=(11.7,  8.3), dpi=72.0)
    ax = fig.add_subplot(111, projection='3d')
    
    data = np.array([[0.0, 0.0, 0.0],
                     [0.0, 0.0, 1.0],
                     [1.0, 0.0, 1.0],
                     [1.0, 1.0, 1.0],
                     [1.0, 1.0, 0.0]])
    
    cs = ax.plot(data[:,0], data[:,1], data[:,2], 'b-')
    
    ax.set_aspect('equal')
    ax.auto_scale_xyz([0.0, 1.0], [0.0, 1.0], [0.0, 1.0])
    
    #plt.draw()
    #ax.apply_aspect()
    
    plt.savefig('c:/temptemp/test_aspect.png')
    plt.show()
    

    The workaround plt.draw() suggested by @efiring worked for me.
    Also is calling the Axes method ax.apply_aspect()

    I am pretty sure that this issue it related to #1104,
    maybe some callback function is not triggered correctly.

Comments are closed.