418

I have made my plots inline on my Ipython Notebook with "%matplotlib inline."

Now, the plot appears. However, it is very small. Is there a way to make it appear larger using either notebook settings or plot settings?

enter image description here

Thomas K
  • 39,200
  • 7
  • 84
  • 86
Chris
  • 28,822
  • 27
  • 83
  • 158

10 Answers10

575

The default figure size (in inches) is controlled by

matplotlib.rcParams['figure.figsize'] = [width, height]

For example:

import matplotlib.pyplot as plt
plt.rcParams['figure.figsize'] = [10, 5]

creates a figure with 10 (width) x 5 (height) inches

Verena Haunschmid
  • 1,252
  • 15
  • 40
tacaswell
  • 84,579
  • 22
  • 210
  • 199
  • 12
    This is actually much more useful as a set-it-and-forget-it strategy if all one's plots are miss-sized. – ijoseph Oct 01 '16 at 19:27
  • 2
    Great. Struggled with pandas boxplot size and this fixed it. For some reason the `figsize=(x,y)` argument is ineffective in jupyter. However, modifying `matplob.rcParams`, as you suggest, works perfectly. – 0_0 Nov 23 '16 at 19:55
  • 43
    Make sure you run this _after_ running `%matplotlib inline`. – uut Nov 22 '17 at 07:32
  • @uut isn't this the default? – Verena Haunschmid Mar 27 '18 at 05:57
  • 8
    @VerenaHaunschmid running `%matplotlib inline` after setting rcParams seems to overwrite the figure size back to default. – uut Mar 28 '18 at 01:22
  • This was perfect solution for a for a quick-and-dirty three line (`nx.write_edgelist(G)` & `H=nx.read_edgelist()` & `nx.draw(H)`) NetworkX plot in Jupyter notebook. – xtian Aug 04 '18 at 15:10
  • You are definitely a Life Saver! Actually, I came from a totally different question and ended up here. After exhausting all the solutions, this one finally works! Really appreciate! – Nick Nick Nick Apr 12 '22 at 01:50
290

Yes, play with figuresize and dpi like so (before you call your subplot):

fig=plt.figure(figsize=(12,8), dpi= 100, facecolor='w', edgecolor='k')

As @tacaswell and @Hagne pointed out, you can also change the defaults if it's not a one-off:

plt.rcParams['figure.figsize'] = [12, 8]
plt.rcParams['figure.dpi'] = 100 # 200 e.g. is really fine, but slower
roadrunner66
  • 7,772
  • 4
  • 32
  • 38
84

I have found that %matplotlib notebook works better for me than inline with Jupyter notebooks.

Note that you may need to restart the kernel if you were using %matplotlib inline before.

Update 2019: If you are running Jupyter Lab you might want to use %matplotlib widget

tomcheney
  • 1,750
  • 1
  • 17
  • 33
  • 5
    Maybe this works in some cases but when I tried this on mine it gave me me a blank image with a pandas dataframe `df.plot()`. I had to go back to `%matplotlib inline` – tsando Sep 29 '17 at 11:00
  • 8
    @tsando I have seen an issue where switching from `%matplotlib inline` to `%matplotlib notebook` without restarting the kernel gives a blank output. Switching from `%matplotlib notebook` to `%matplotlib inline` works fine. – tomcheney Sep 29 '17 at 13:58
  • 4
    Thanks @SlimCheney, I confirm what you say is true - it works if you restart the kernel – tsando Sep 29 '17 at 16:10
  • 1
    This definitely beats trying to set up panning and zoom components by hand – beldaz Jan 23 '18 at 08:08
  • 1
    it does not work, even after restart the kernel. It gives a blank output – Kardi Teknomo Jan 01 '19 at 13:23
  • Any way to turn off interactive mode by default? – saiedmomen Feb 27 '19 at 21:45
  • 2
    Note that %matplotlib notebook no longer works with Jupyter Lab – Daniel Kats Apr 05 '20 at 17:07
  • @DanielKats I actually updated this answer in October 2019 to point out that in Jupyter Labs you should use %matplotlib widget – tomcheney Apr 06 '20 at 08:56
  • 2
    I had lots of trouble with `%matplotlib widget` until I followed [the instructions on the README](https://github.com/matplotlib/ipympl#installation) (particularly `jupyter labextension install @jupyter-widgets/jupyterlab-manager`). – Matthew D. Scholefield Nov 18 '20 at 12:54
58

If you only want the image of your figure to appear larger without changing the general appearance of your figure increase the figure resolution. Changing the figure size as suggested in most other answers will change the appearance since font sizes do not scale accordingly.

import matplotlib.pylab as plt
plt.rcParams['figure.dpi'] = 200
Hagne
  • 8,470
  • 2
  • 18
  • 17
29

The question is about matplotlib, but for the sake of any R users that end up here given the language-agnostic title:

If you're using an R kernel, just use:

options(repr.plot.width=4, repr.plot.height=3)
Jared Wilber
  • 6,038
  • 1
  • 32
  • 35
19

To adjust the size of one figure:

import matplotlib.pyplot as plt

fig=plt.figure(figsize=(15, 15))

To change the default settings, and therefore all your plots:

import matplotlib.pyplot as plt

plt.rcParams['figure.figsize'] = [15, 15]

farrellw
  • 1,340
  • 15
  • 12
8

It is possible to scale the plot to full cell width.

  1. Use svg format instead of bitmap when mainly plotting line charts:
%config InlineBackend.figure_format = 'svg'
  1. Force the plot to be 100% width (paste into an empty cell):
%%html
<style>
.output_svg div{
  width: 100% !important;
  height: 100% !important;
}
</style>
  1. You may also want to change the aspect ratio or other parameters according to other answers for better insight.

It is not using public API and may stop working one day. screenshot of a full-width matplotlib plot

Meow Cat 2012
  • 928
  • 1
  • 9
  • 21
7

using something like:

import matplotlib.pyplot as plt
%matplotlib inline
plt.subplots(figsize=(18,8 ))
plt.subplot(1,3,1)
plt.subplot(1,3,2)
plt.subplot(1,3,3)

The output of the command

the output of the command

Ajanyan Pradeep
  • 1,097
  • 1
  • 16
  • 26
Bijan Fallah
  • 71
  • 1
  • 3
4

A small but important detail for adjusting figure size on a one-off basis (as several commenters above reported "this doesn't work for me"):

You should do plt.figure(figsize=(,)) PRIOR to defining your actual plot. For example:

This should correctly size the plot according to your specified figsize:

values = [1,1,1,2,2,3]
_ = plt.figure(figsize=(10,6))
_ = plt.hist(values,bins=3)
plt.show()

Whereas this will show the plot with the default settings, seeming to "ignore" figsize:

values = [1,1,1,2,2,3]
_ = plt.hist(values,bins=3)
_ = plt.figure(figsize=(10,6))
plt.show()
RGood
  • 111
  • 1
  • 6
  • This is such an important point that I think someone should edit the answer to reflect this. This is all I wanted and couldn't for the life of me figure out why this was not working. – ejkitchen Mar 22 '22 at 01:13
0

A quick fix to "plot overlap" is to use plt.tight_layout():

Example (in my case)

for i,var in enumerate(categorical_variables):
    plt.title(var)
    plt.xticks(rotation=45)
    df[var].hist()
    plt.subplot(len(categorical_variables)/2, 2, i+1)

plt.tight_layout()