Spyder Plot Inline - python

Setup: Anaconda 2.0.0 (Win 64), Spyder (2.3.0rc that came with Anaconda)
I configure the graphics:
Tools > Preferences > iPython console > Graphics > Graphics backend > Inline
But no matter what I do the graphics always open in a separate window? Is there a way to force them to be inline in the console?

From Spyder 4.0, figures now render in the new Plots pane by default, so the answer in the comments is outdated - for the benefit of people viewing this question in the future; figures can now be plotted inline by unchecking the "Mute Inline Plotting" option from the menu in the top-right of the Plots pane, as shown below.

If you are using the latest version of spyder, you just change "variable explore" to "plots", which shown above the console.

Related

Interactive plots inside Spyder ipython console

Is it possible to plot interactive graphics with sliders and buttons directly inside the Spyder IPython console and not in a separated window ? I find the inline mode really convenient but no interaction seems possible yet. I tried %matplotlib notebook but an error occurred. Any tips please ?
Thanks !!
(Spyder maintainer here) No, unfortunately that's not possible.

Python: Spyder figure window - can't copy or save

I am running Spyder v3.2.4 in Windows 10.
I have gone to Tools > Preferences > IPython console > Graphics > Graphics Backend > Automatic to get my graphs/figures in separate windows. My question is, can I change the window type used to display these graphics? For example, in the window style I have I cannot copy or print the graphic directly. Instead having to save first. Is there a way around this?
The window style I have is:
(Spyder maintainer here) Plot windows are not generated nor handled by Spyder but by Matplotlib. Unfortunately (as you correctly point out) you can only save plots in them.

Plot plotted inside the ipython console

I am working in python and using Anaconda with Spyder and I need to plot a scatter plot in a part of the code I am working on. The plot is just fine but is drawn in the console itself and the size is quite small for my needs.
My question is, how can I plot it in another window outside the ipython console? I already tried changing some of the setting like changing Graphics setting in ipython from inline to QT to tkinter. Nothing seems to work.
Any helpful suggestions?
you can just save %matplotlib qt in a .py file and you can start ipython using:
ipython -i myfile.py

Plot inline or a separate window using Matplotlib in Spyder IDE

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!
type
%matplotlib qt
when you want graphs in a separate window and
%matplotlib inline
when you want an inline plot
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
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.
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.)
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
select the 'plots' tab
click the button with 3 horizontal bars
select 'undock'
It'll open the plot in a new window. When you close the window, it docks back.

Interactive matplotlib through Eclipse PyDev

This is a follow up to this interactive matplolib through eclipse thread which is about 2 years old, I was wondering if there has been any progress in the meantime.
I am running the IPython console in a console window in Eclipse PyDev, but I am unable to get the same interactive plotting features with matplotlib as if I were to run IPython in a (Windows) command prompt outside Eclipse PyDev. Here is how the two compare:
A) Running IPython in a shell outside Eclipse PyDev
Run IPython in a Windows command prompt with "ipython --pylab"
Within the IPython console enter "plot([1,2,3])". This will open a figure plot window and the IPython console is ready for further commands (without having to close the figure plot window).
For example, I can enter "xlabel('years')" and this will update my figure plot window.
B) Running IPython in an interactive console within Eclipse PyDev
Enter the following in the IPython interactive console within Eclipse PyDev:
"from pylab import *"
"plot([1,2,3])" --> Figure plot window does not show up.
I have to enter "show()" to open the figure plot window. But now the problem is that as long as I keep the figure plot window open, the IPython console does not accept any new commands.
So I close the figure plot window, enter "xlabel('years')", followed by "show()" again. This will re-open the figure plot window with "years" as my axis label, but the plot itself is empty and does not show the [1,2,3] data anymore.
With this behaviour, A) is clearly superior to B), but I would like to keep working in Eclipse PyDev because I like always having the variables list on my screen (without having to run a command to show all variables like when running IPython form a windows shell). Using Wicked Shell, as suggested in the other thread, does not work (IPython does not work properly in Wicked Shell).
How can I configure IPython in Eclipse PyDev so that it shows the same interactive behavior as if I would run it in a windows command prompt?
You can solve this problem by selecting a GUI for the Interactive Console in PyDev Preference.
Eclipse -> Window -> Preferences -> Pydev -> Interactive Console -> Enable GUI event loop integration.
In my case, I chose PyQt (qt/qt4)
Apologies for the potentially incomplete answer, but hopefully I will be able to shed some light on the problem.
I believe that the one that the OP describes is normal behaviour. In fact, starting from the command line ipython, importing pylab and issuing a plot command produces exactly the blocking behaviour described, so this is not related to pydev or eclipse. The fact is that show in matplotlib is blocking in interactive mode; when you use matplotlib in a ipython session started as "ipython --pylab", you are taking advantage of some "hacks" that the ipython developers did for you around matplotlib, allowing to have both an interactive mode and non blocking calls. However, importing pylab is not enough to apply these "hacks". PyDev does not seems to allow flags to the interpreter call, so one can't directly invoke "ipython --pylab".
Luckily, ipython has a special command "pylab" that applies the hacks and imports pylab even if the interpreter was not started with the pylab flag. So you can just try to type "pylab" inside the console (actually, you can even customize your pydev console so that it is done automatically) and you should get the desired behaviour. However, I must report that while this works fine for me from a ipython session started from the command line, something goes wrong when I try to do the same from inside Eclipse. The command doesn't block, I get the python icon but the matplotlib window doesn't show up. For the records, I am on a Mac running Snow Leopard. I am not able to tell if the same problem happens also in Windows, that the OP seems to be using.
I achieve similar behave in Eclipse PyDev by executing plotting function in another thread:
import threading
from pylab import *
import matplotlib.animation as animation
import time
x = array(range(0,1000))/100
y = sin(x)
def updateData(self):
ax.set_data(x,y)
def MyThread():
global ax
fig, axarr = subplots(1)
ax, = axarr.plot(x,y)
simulation = animation.FuncAnimation(fig, updateData)
show()
t = threading.Thread(target=MyThread)
t.start()
# console stay active, user can interactively control figure
time.sleep(1)
y = sin(2*x)
time.sleep(2)
ax.get_axes().grid()
ax.get_axes().set_xlabel("time")
Tested with toolchain Eclipse 4.3, PyDev 2.7.1, Python 3.2, IPython 0.13
Just use the %matplotlibmagic-command to activate interactive plotting (exactly what you described).
The pylab command imports numpy.* and pylab.*, seriously polluting your global namespace.

Categories

Resources