Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib PDF plots don't respond to figsize

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.

like image 332
nivek Avatar asked Feb 27 '14 20:02

nivek


People also ask

How does Figsize work matplotlib?

The figsize attribute is a parameter of the function figure(). It is an optional attribute, by default the figure has the dimensions as (6.4, 4.8). This is a standard plot where the attribute is not mentioned in the function. Normally each unit inch is of 80 x 80 pixels.

Why is matplotlib not working?

Occasionally, problems with Matplotlib can be solved with a clean installation of the package. In order to fully remove an installed Matplotlib: Delete the caches from your Matplotlib configuration directory. Delete any Matplotlib directories or eggs from your installation directory.

How do you change Figsize in matplotlib?

Import matplotlib. To change the figure size, use figsize argument and set the width and the height of the plot. Next, we define the data coordinates. To plot a bar chart, use the bar() function. To display the chart, use the show() function.

What is matplotlib default Figsize?

figsize"] (default: [6.4, 4.8]) = [6.4, 4.8] . dpiinteger, optional, default: None. resolution of the figure. If not provided, defaults to rcParams["figure. dpi"] (default: 100.0) = 100 .


1 Answers

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()
like image 120
holdenweb Avatar answered Oct 02 '22 10:10

holdenweb