Plot inline or a separate window using Matplotlib in Spyder IDE

PythonMatplotlibSpyder

Python Problem Overview


When I use Matplotlib to plot some graphs, it is usually fine for the default inline drawing. However, when I draw some 3D graphs, I'd like to have them in a separate window so that interactions like rotation can be enabled. Can I configure in Python code which figure to display inline and which one to display in a new window?

I know that in Spyder, click Tools, Preferences, Ipython Console, Graphics and under Graphics Backend select “automatic” instead of “inline”. However, this make all the figures to be in new windows. It can be messy when I have a lot of plots. So I want only those 3D plot to be in new windows, but all the other 2D plots remain inline. Is it possible at all?

Thanks!

Python Solutions


Solution 1 - Python

type

%matplotlib qt

when you want graphs in a separate window and

%matplotlib inline

when you want an inline plot

Solution 2 - Python

Go to Tools >> Preferences >> IPython console >> Graphics >> Backend:Inline, change "Inline" to "Automatic", click "OK"

Reset the kernel at the console, and the plot will appear in a separate window

Solution 3 - Python

Magic commands such as

%matplotlib qt  

work in the iPython console and Notebook, but do not work within a script.

In that case, after importing:

from IPython import get_ipython

use:

get_ipython().run_line_magic('matplotlib', 'inline')

for inline plotting of the following code, and

get_ipython().run_line_magic('matplotlib', 'qt')

for plotting in an external window.

Edit: solution above does not always work, depending on your OS/Spyder version Anaconda issue on GitHub. Setting the Graphics Backend to Automatic (as indicated in another answer: Tools >> Preferences >> IPython console >> Graphics --> Automatic) solves the problem for me.

Then, after a Console restart, one can switch between Inline and External plot windows using the get_ipython() command, without having to restart the console.

Solution 4 - Python

I have set the IPython console backend set to Automatic in the Spyder preferences.

In my scripts, I can now use switch_backend as either plt.switch_backend('module://ipykernel.pylab.backend_inline') or plt.switch_backend('Qt5Agg') before each new plot, to make it either inline or separate/interactive.

(Tested with Spyder 4.2.2.)

Solution 5 - Python

If you want to look at just 1 or 2 charts you can also try manually undocking them in the plot window. So on the top right corner window

  1. select the 'plots' tab
  2. click the button with 3 horizontal bars
  3. select 'undock'

It'll open the plot in a new window. When you close the window, it docks back.

enter image description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpyanView Question on Stackoverflow
Solution 1 - PythonStefano FedeleView Answer on Stackoverflow
Solution 2 - PythonAuHgNeMView Answer on Stackoverflow
Solution 3 - PythonDon ErnestoView Answer on Stackoverflow
Solution 4 - PythonRobert PollakView Answer on Stackoverflow
Solution 5 - Pythonvine_JView Answer on Stackoverflow