Matplotlib PDF plots don't respond to figsize

matplotlib, python

Solution

You might find it more convenient to use `matplotlib.pyplot.figure`

Try configuring the figure width after you have created it with code like

fig = plt.figure()
fig.set_figheight(5)
fig.set_figwidth(8)

I may have the dimensions transposed, but this works for me. Here's a complete example cribbed from the matplotlib documentation with modifications to the figure size. This also works with a `figsize` parameter to the figure() call.

from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
fig.set_figheight(10)
fig.set_figwidth(12)
ax = fig.gca(projection='3d')
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm,
        linewidth=0, antialiased=False)
ax.set_zlim(-1.01, 1.01)

ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))

fig.colorbar(surf, shrink=0.5, aspect=5)
fig.savefig("myfig.png", dpi=600) # useful for hi-res graphics
plt.show()

Problem

I'd like the following code to size subplots such that the resulting PDF is 5 inches wide and 8 inches tall. But no matter what I put in the `figsize` bit, the resulting file is 8 inches wide and 6 inches tall. What am I doing wrong? ``` import matplotlib.pyplot as plt import matplotlib.gridspec as gs fig = plt.Figure(figsize=(5,8)) fig.set_canvas(plt.gcf().canvas) gs1 = gs.GridSpec(3,2) gs1.update(wspace=0.4,hspace=0.4) ax1 = plt.subplot(gs1[0,0]) ax2 = plt.subplot(gs1[0,1]) ax3 = plt.subplot(gs1[1,0]) ax4 = plt.subplot(gs1[1,1]) ax5 = plt.subplot(gs1[2,:]) ax1.plot([1,2,3],[4,5,6], 'k-') fig.savefig("foo.pdf", format='pdf') ``` Oops---edited to add that I have also tried `fig.set_size_inches((5,8))` and this doesn't seem to have any effect either.

Original source