Categories
graph matplotlib plot python visualization

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

2939

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

0

    1041

    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)
    

    5

    • 6

      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).

      Nov 26, 2012 at 14:21

    • 16

      Similarly, you can run fig.set_dpi(100).

      Mar 27, 2015 at 19:07

    • 2

      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.

      Jan 7, 2020 at 20:12

    • 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

      Jan 21 at 17:56

    • should be called before .show()

      – NoamG

      Jul 6 at 9:18

    643

    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.

    11

    • 7

      Excellent! Also useful for setting it once and plotting multiple times.

      Oct 18, 2017 at 22:44

    • 3

      For some reason, it seems to this no longer works in Jupyter notebook (but used to).

      – Ray

      Dec 13, 2017 at 14:01

    • @Ray Can you write the version of your Jupyter notebook and the behaviour for me it works

      – G M

      Dec 13, 2017 at 16:41

    • 19

      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.

      – asmaier

      May 11, 2018 at 7:33

    • 3

      To reset the global figure size for subsequent plots back to default use plt.rcParams['figure.figsize'] = plt.rcParamsDefault['figure.figsize']

      – Eiron

      May 11, 2021 at 21:46