PeterB
PeterB

Reputation: 2424

Using %matplotlib notebook after %matplotlib inline in Jupyter Notebook doesn't work

I am using Jupyter Notebook for plotting piechart figures.

In first cell with my code I have a magic command %matplotlib inline and after this magic command I run my code, everything works fine and my figure renders.

But in second cell when I set %matplotlib notebook for interactive plotting my figure won't render after running this second cell.

I need to restart kernel and run cell with %matplotlib notebook again and cannot run %matplotlib inline command before that.

Here is my code for first cell with %matplotlib inline, which renders fine:

import matplotlib.pyplot as plt

%matplotlib inline

labels = "No", "Yes"
sizes = [100, 50]

fig, ax = plt.subplots(figsize=(6, 6))

_, texts, autotexts = ax.pie(sizes, explode=explode, labels=labels, colors=colors, autopct='%1.1f%%',
             shadow=False, startangle=90)

ax.axis('equal')

After that I have second cell with same code, just %matplotlib inline is changed to %matplotlib notebook. Figure won't render after I run this cell and I need to restart kernel and run this cell again.

Why?

Upvotes: 43

Views: 118995

Answers (4)

divenex
divenex

Reputation: 17236

Update for 2024 JupyterLab and Jupyter Notebook

The currently accepted answer did not work for me in JupyterLab v4.2. It seems things have changed since it was written some years ago.

I give what I found to be presently working, to give interactive Matplotlib plots:

  • The %matplotlib <backend> magic command is enough;
  • The name of the interactive backend is widget or ipympl (the former is an alias of the latter). The ipyml package must be installed.
  • One has to close the figure when switching back to the inline backend.

Here is a minimal example:

First cell

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 7, 100)
plt.plot(np.sin(x))

enter image description here

Second cell

%matplotlib widget

plt.plot(np.sin(2*x))
plt.show()

enter image description here

Third cell

%matplotlib inline

plt.close()
plt.plot(np.sin(3*x))

enter image description here

Important

No new plot is produced if I comment the plt.close() command in the third cell. Instead, the second cell is updated with the output of the third cell as follows.

enter image description here

Upvotes: 1

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339580

You just have the wrong order of your commands. A backend should be set before importing pyplot in jupyter. Or in other words, after changing the backend, pyplot needs to be imported again.

Therefore call %matplotlib ... prior to importing pyplot.

In first cell:

%matplotlib inline
import matplotlib.pyplot as plt
plt.plot([1,1.6,3])

In second cell:

%matplotlib notebook
#calling it a second time may prevent some graphics errors
%matplotlib notebook  
import matplotlib.pyplot as plt
plt.plot([1,1.6,3])

Upvotes: 54

Michelle Abaya
Michelle Abaya

Reputation: 7

In Jupyter notebook, you have to enter matplotlib notebook in the same line as the one you want to run. Even if you enter "inline" then followed by "notebook", it still won't work. It has to be on the same line as the code you want to render.

Upvotes: -1

unddoch
unddoch

Reputation: 6004

Edit: turns out that you can in fact change backends dynamically on jupyter. Still leaving the answer here because I think it's relevant and explains some matplotlib magic that can pop out sometimes.

The magic command, as seen in the source code, is calling matplotlib.pyplot.switch_backend(newbackend) to change the backend. As stated in matplotlib's docs:

matplotlib.pyplot.switch_backend(newbackend)

Switch the default backend. This feature is experimental, and is only expected to work switching to an image backend. e.g., if you have a bunch of PostScript scripts that you want to run from an interactive ipython session, you may want to switch to the PS backend before running them to avoid having a bunch of GUI windows popup. If you try to interactively switch from one GUI backend to another, you will explode..

So you really have to restart the kernel each time you switch backends, because matplotlib has a problem to switch the backend after being used.

This problem is mainly due to incompatibilities between different main-loops of the GUI backend. Because normally each backend is also taking care of threads and user input you can't run Qt and Tkinter side-by-side. So that limitation is carried over to jupyter.

Also see this question: How to switch backends in matplotlib / Python

Upvotes: 6

Related Questions