Plot inline or a separate window using Matplotlib in Spyder IDE

207,484

Solution 1

type

%matplotlib qt

when you want graphs in a separate window and

%matplotlib inline

when you want an inline plot

Solution 2

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

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.

Share:
207,484
pyan
Author by

pyan

A research scientist developing image analysis algorithms with programming skills in C/C++, Java, Python, and Matlab.

Updated on January 23, 2022

Comments

  • pyan
    pyan over 2 years

    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!

  • Tobbey
    Tobbey almost 6 years
    Do you know if it is possible tosetup this option in some spyderrc config file somewhere ?
  • Tobbey
    Tobbey almost 6 years
    Edit, I found it: /home/user/.config/spyder-py3/spyder.ini it should contain pylab/backend = 1 I think
  • MasterControlProgram
    MasterControlProgram almost 5 years
    +1 Under Ubuntu and running Jupyter / IPython under firefox, the last line get_ipython().run_line_magic('matplotlib', 'qt') is necessary to open an interactive window with a matplotlib-plot!
  • Hansang
    Hansang over 4 years
    Also works for vscode python interactive window, where the magic commands don't always work
  • Ben
    Ben over 4 years
    is there an option to show the plots whether in a new opening window nor within the console, e.g., in a separate window like in Rstudio? E.g. when plotting 10 plots it is quite annoying to have 10 opening windows but also to scroll through the entire console all the time. Hence, I hope it is possible to address them to a certain window but I can't figure out how..?
  • Vinigas
    Vinigas about 4 years
    Where should I type it in Python Spyder ?