102

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

5 Answers 5

110

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

8
  • 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
    Commented May 19, 2017 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
    Commented Dec 11, 2017 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.. Commented Dec 12, 2017 at 16:09
  • 4
    You may need to restart your notebook if you already called %matplotlib inline
    – Guido
    Commented Feb 6, 2018 at 12:28
  • 16
    I get the error "Javascript Error: IPython is not define"
    – Ben
    Commented Oct 10, 2019 at 7:33
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 :)

5
  • It seems 3D plots are not supported yet : github.com/jakevdp/mpld3/issues/223
    – Théo T
    Commented Dec 2, 2014 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.
    – user2321808
    Commented Sep 24, 2015 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)) Commented Sep 6, 2016 at 19:15
  • 7
    Doesn't work for me. I'm getting Json serialization error
    – Gulzar
    Commented Nov 23, 2018 at 13:39
  • 1
    It rather slowed down my kernel... extremely.
    – Sachin
    Commented Apr 27, 2022 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
3
  • 3
    I honestly think this is the best answer, looks way better than mpld3 and more features.
    – bznein
    Commented Jan 20, 2018 at 15:37
  • This did the trick for me. I agree with @bznein in the sense it is cleaner, works by default and does the trick.
    – Joop
    Commented Mar 11, 2022 at 15:47
  • With this option, plots do not even show up for me
    – Rubi Shnol
    Commented May 12, 2023 at 18:43
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.

1
  • 1
    Yes it is - with a recent version of matplotlib, you can now select the nbagg backend.
    – Thomas K
    Commented Dec 4, 2014 at 22:09
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

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.