99

Is it possible to zoom into a plot if inline is activated? Especially regarding to 3d-plots rotating and zooming is a necessary feature.

varantir
  • 6,624
  • 6
  • 36
  • 57

6 Answers6

110

You can now use %matplotlib notebook instead of %matplotlib inline and you'll be able to interact with your plots.

João Abrantes
  • 4,772
  • 4
  • 35
  • 71
  • 1
    This a great solution. If you try it and it doesn't work, check your version of matplotlib. I think this was introduced in version 1.5 – jpobst May 19 '17 at 14:14
  • Doesn't work as well as I'd hoped with subplots; can zoom and pan for an individual subplot but can't increase/decrease the size of the entire plot image. Which, to be fair, wasn't the original question asked above. – ijoseph Dec 11 '17 at 22:49
  • 1
    @ijoseph you can increase the size of the entire image as well. Just drag the right bottom corner of the plot.. – João Abrantes Dec 12 '17 at 16:09
  • @JoãoAbrantes ah. I didn't see that because my initial `figsize` was set to be so large that the resizing corner was off the visible area of the page. An eventual [workaround was increasing the width of the notebook area entirely](https://stackoverflow.com/a/34058270/588437). – ijoseph Dec 12 '17 at 22:15
  • 4
    You may need to restart your notebook if you already called %matplotlib inline – Guido Feb 06 '18 at 12:28
  • I have 4 charts in my notebook, in separate cells. This places the last executed chart in first drawn one. Anyone else facing this? My matplot v2.1.1 – Mahendran Feb 22 '18 at 07:02
  • 15
    I get the error "Javascript Error: IPython is not define" – Ben Oct 10 '19 at 07:33
  • @Ben: This is covered in [this question](https://stackoverflow.com/a/56416229/1804173). Looks like `%matplotlib notebook` is outdated, and `ipympl` + `%matplotlib widget` is the modern approach. – bluenote10 Jun 08 '23 at 08:42
103

Now thanks to mpld3 it's super easy to enable zooming in inline plots!

All you have to do is install mpld3 (pip install mpld3), and then add this to your notebook:

%matplotlib inline
import mpld3
mpld3.enable_notebook()

Now your plots will get a toolbar menu at the bottom left, in which you can enable mouse zooming :)

yonilevy
  • 5,320
  • 3
  • 31
  • 27
  • It seems 3D plots are not supported yet : https://github.com/jakevdp/mpld3/issues/223 – Théo T Dec 02 '14 at 17:14
  • 3
    This is great, but one should add that with some of the heavier duty plots (plots containing millions of points, for instance), it is very slow. –  Sep 24 '15 at 15:00
  • 2
    I implemented this in my Jupyter Notebook 4.2.2 and zooming in caused the figure to go blank. Adding origin='lower' as an argument to the imshow() call fixed it. (e.g. imshow(res, origin='lower', cmap = cm.gray)) – DanGoodrick Sep 06 '16 at 19:15
  • 7
    Doesn't work for me. I'm getting `Json serialization error` – Gulzar Nov 23 '18 at 13:39
  • 1
    It rather slowed down my kernel... extremely. – Sachin Apr 27 '22 at 10:25
14

mpld3 slowed down the execution of my notebooks. I found it better to use the nbagg backend that provides the same interactive tools but also allows to save graphs by the right-click menu:

import matplotlib
matplotlib.use('nbagg')
import matplotlib.pyplot as plt
10

At present, the closest you can come is to redraw it at a larger size using the figsize function. It expects dimensions in inches, which caught me out the first time I tried to use it.

There are some plants for a rich backend that would allow plots to be manipulated live, using HTML5, but I think it will be a few more months before that's ready.

If you're using the notebook on your local computer, for now the easiest option might be not to use inline mode, so the plots pop up as separate windows.

Thomas K
  • 39,200
  • 7
  • 84
  • 86
7

Another good example that has emerged recently is to outsource the job to plotly:

https://plot.ly/python/3d-plots-tutorial/

Let them handle the rendering, panning, and zooming for you!

Andrew Mao
  • 35,740
  • 23
  • 143
  • 224
0

matplotlib.use('nbagg') didnt work for me either. I did find mdplt3 quite slow. Instead of zooming, I ended up resizing my figure (making it big), using this post: Plot width settings in ipython notebook

Ruta Desai
  • 81
  • 1
  • 5