How do I change the size of figures drawn with Matplotlib?

4,843,368

Solution 1

figure tells you the call signature:

from matplotlib.pyplot import figure

figure(figsize=(8, 6), dpi=80)

figure(figsize=(1,1)) would create an inch-by-inch image, which would be 80-by-80 pixels unless you also give a different dpi argument.

Solution 2

If you've already got the figure created, you can use figure.set_size_inches to adjust the figure size:

fig = matplotlib.pyplot.gcf()
fig.set_size_inches(18.5, 10.5)
fig.savefig('test2png.png', dpi=100)

To propagate the size change to an existing GUI window, add forward=True:

fig.set_size_inches(18.5, 10.5, forward=True)

Additionally as Erik Shilts mentioned in the comments you can also use figure.set_dpi to "[s]et the resolution of the figure in dots-per-inch"

fig.set_dpi(100)

Solution 3

Using plt.rcParams

There is also this workaround in case you want to change the size without using the figure environment. So in case you are using plt.plot() for example, you can set a tuple with width and height.

import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = (20,3)

This is very useful when you plot inline (e.g., with IPython Notebook). As asmaier noticed, it is preferable to not put this statement in the same cell of the imports statements.

To reset the global figure size back to default for subsequent plots:

plt.rcParams["figure.figsize"] = plt.rcParamsDefault["figure.figsize"]

Conversion to cm

The figsize tuple accepts inches, so if you want to set it in centimetres you have to divide them by 2.54. Have a look at this question.

Solution 4

Deprecation note:
As per the official Matplotlib guide, usage of the pylab module is no longer recommended. Please consider using the matplotlib.pyplot module instead, as described by this other answer.

The following seems to work:

from pylab import rcParams
rcParams['figure.figsize'] = 5, 10

This makes the figure's width 5 inches, and its height 10 inches.

The Figure class then uses this as the default value for one of its arguments.

Solution 5

Please try a simple code as following:

from matplotlib import pyplot as plt
plt.figure(figsize=(1,1))
x = [1,2,3]
plt.plot(x, x)
plt.show()

You need to set the figure size before you plot.

Share:
4,843,368
tatwright
Author by

tatwright

Updated on April 25, 2022

Comments

  • tatwright
    tatwright about 2 years

    How do I change the size of figure drawn with Matplotlib?

  • heltonbiker
    heltonbiker over 11 years
    Solved me a problem with imshow, now I'm using this code just after eliminating the space around the plotting area with plt.subplots_adjust(left=0.0, right=1.0, bottom=0.0, top=1.0).
  • drevicko
    drevicko almost 11 years
    If you've already got the figure created, say it's 'figure 1' (that's the default one when you're using pyplot), you can use figure(num=1, figsize=(8, 6), ...) to change it's size etc. If you're using pyplot/pylab and show() to create a popup window, you need to call figure(num=1,...) before you plot anything - pyplot/pylab creates a figure as soon as you draw something, and the size of the popup appears to be fixed at this point.
  • nealmcb
    nealmcb over 10 years
    This also works nicely at the top of a iPython notebook, which (given --pylab=inline) has rcParams already imported at the top level.
  • Frames Catherine White
    Frames Catherine White over 9 years
    This answer tells me that that it is matplotlib.pyplot.figure, which the others do not say as clearly. I keep trying things like matplotlib.figure and matplotlib.figure.Figure
  • Erik Shilts
    Erik Shilts about 9 years
    Similarly, you can run fig.set_dpi(100).
  • Bennett Brown
    Bennett Brown over 8 years
    This did not work on my Windows machine with OO interface to pyplot and Qt backend. fig.set_size_inches(18.5, 10.5, forward=True) worked.
  • y.selivonchyk
    y.selivonchyk over 7 years
    Every time I am trying to recall how to do it I end up in this post. So, this is the code i am normally looking for: fig = plt.figure() default_size = fig.get_size_inches() fig.set_size_inches( (default_size[0]*2, default_size[1]*2) )
  • Cerin
    Cerin about 7 years
    "_tkinter.TclError: not enough free memory for image buffer"
  • Mischa
    Mischa almost 7 years
    This answer is currently discussed on Meta
  • CMCDragonkai
    CMCDragonkai almost 7 years
    Does this mean if you set DPI to 1, then figsize becomes a pixel unit instead of an inch unit? This would be more useful when using matplotlib on web and GUI interfaces.
  • automorphic
    automorphic over 6 years
    Excellent! Also useful for setting it once and plotting multiple times.
  • Breno Baiardi
    Breno Baiardi over 6 years
    with figsize(1,1) you will have a ratio in the image as 1:1? because all my pie charts are show oval, and the only way I found to make them round was by using plot.axis("equals"). they would have the same effect or they behave diferently?
  • Jouni K. Seppänen
    Jouni K. Seppänen over 6 years
    @BrenoBaiardi This question is about the figure size. The axes on top of the figure may still have an unequal aspect ratio even if the figure as a whole is 1:1, and even if the axes box is 1:1, the data may be scaled differently in the x and y directions. So, no, that command will not guarantee an equal aspect ratio.
  • ximiki
    ximiki over 6 years
    plt.figure(figsize=(1,1)) is the crux move. Thank you.
  • Ray
    Ray over 6 years
    For some reason, it seems to this no longer works in Jupyter notebook (but used to).
  • G M
    G M over 6 years
    @Ray Can you write the version of your Jupyter notebook and the behaviour for me it works
  • Ray
    Ray over 6 years
    @GM last time I tested, it was Jupyter version 4.3.0 and Python 3.6.2 from Anaconda, on Windows 10 and Google Chrome 63.0.3239.84.
  • asmaier
    asmaier almost 6 years
    To make it work I need to call plt.rcParams["figure.figsize"] = (20,3) in an extra cell. When I call it in the same cell as the import statement, it gets ignored.
  • ivanleoncz
    ivanleoncz over 5 years
    Very pragmatic, just like what the doc says: matplotlib.org/api/_as_gen/matplotlib.pyplot.figure.html Just one thing to observe: the values of this tuple are Width and Height, respectively
  • Steve Gon
    Steve Gon over 5 years
    For Python version: 3.6.4, matplotlib: 2.2.3 I think you need to pass a list or tuple e.g. rcParams['figure.figsize'] = (5, 10)
  • Stefano
    Stefano about 5 years
    Note that if you set a too large number in figsize, the figure will only maximize to until the edges of the screen. This opens the possibility for a horizontal maximization and a vertical maximization.
  • Abhishek Poojary
    Abhishek Poojary almost 5 years
    In jupyter notebook, this worked for me : plt.figure(figsize=(20,10))
  • Alberto Chiusole
    Alberto Chiusole almost 5 years
    @asmaier I noticed the same behavior. Moreover, if you simply want to double/N-multiply the size and numpy is available, you can do: plt.rcParams["figure.figsize"] = np.array(plt.rcParams["figure.figsize"]) * 3
  • g07kore
    g07kore over 4 years
    Anyone knows why this does not work at the first time running the cell?
  • Deepak Rajendran
    Deepak Rajendran over 4 years
    I use this extensively in my workflow. I set the plt.rcParams['figure.figsize'] = (w, h) to set a global size for all plots, and change individual figure sizes on a case-to-case basis using the plt.figure(figsize=(w, h)).
  • gosuto
    gosuto over 4 years
    @g07kore there's an outstanding bug related to that: github.com/ipython/ipython/issues/11098
  • Yuhang Lin
    Yuhang Lin over 4 years
    For those using jupyter notebook, please make sure you are using plt.figure(figsize=(15, 10)) before other plt commands.
  • Łukasz Rajchel
    Łukasz Rajchel over 4 years
    A quick way to scale, say, only a vertical size:plt.rcParams["figure.figsize"][1] *= 1.5 fig, axs = plt.subplots(nrows = 2, sharex = True)
  • K. Nielson
    K. Nielson over 4 years
    I'm using this on OS X under ipython 3.7, matplotlib 3.0.3. I am able to adjust the figure size, but the window does not resize (the figure canvas adjusts, but the window does not grow or shrink to accommodate the canvas). The forward=True keyword argument does not appear to have the intended effect on this platform.
  • BachT
    BachT almost 4 years
    copied your snippet too many times, so I leave this comment here for whom just want a copy-paste: plt.rcParams['figure.figsize'] = 5, 10
  • Wayne Workman
    Wayne Workman almost 4 years
    This example is complete, showing how this ties into pyplot.plot() and pyplot.show()
  • Eric Duminil
    Eric Duminil over 3 years
    @g07kore: I think it works when both lines (import and figsize) are in separate cells. Don't ask me why.
  • Jakob
    Jakob over 3 years
    This is very useful as it assigns the size globally. That way you don't need to specify it for each plot. However, it might be more common to do import matplotlib.pyplot as plt and then do plt.rc('figure', figsize=(10,5)).
  • Eiron
    Eiron almost 3 years
    To reset the global figure size for subsequent plots back to default use plt.rcParams['figure.figsize'] = plt.rcParamsDefault['figure.figsize']
  • G M
    G M almost 3 years
    @Eiron that's a good trick feel free to edit the answer if you want
  • Rainald62
    Rainald62 over 2 years
    @Stefano This applies to figures shown on the screen. I frequently save lengthy signal histories with figsize=(50,4) inches (to be viewed later with IrfanView).
  • Samuel
    Samuel over 2 years
    Window resize wasn't working for me. It looked like forward=True was key, but it didn't fix it. Turns out it did fix it, I just to be careful about the order that set_size_inches was called. It needed to be called after the call to tight_layout(). I had this issue with an axes.bar plot, I didn't have it with regular plots.
  • Samuel
    Samuel over 2 years
    Window resize wasn't working for me. It looked like forward=True was key, but it didn't fix it. Turns out it did fix it, I just to be careful about the order that set_size_inches was called. It needed to be called after the call to tight_layout(). I had this issue with an axes.bar plot, I didn't have it with regular plots.