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
Related
I have been running into a trouble whereby Spyder IPython console is not producing Matplotlib figures as desired. I thought initially that there is something wrong in my code since jupyter notebook gives me the same wrong figures. However, when running the script in Spyder using external terminal the figures are produced as desired. Also, when I run the code in VSC the correct figures are displayed.
So the only option I am left with in Spyder is to use the external terminal to execute the code. However, it is quite a pain every time to run some codes and then manually close the terminal.
I would like to know if there is a way to permanently attach the external terminal inside Spyder? I hate the IPython console when it comes to plotting matplotlib figures!!
(Spyder maintainer here) Sorry but there's no way to dock an external Python terminal inside Spyder.
I'm running Python v2.7 and matplotlib v1.5.0 on Mac OS X Yosemite. Up to recently, I was able to run my script in the interactive interpreter, show a plot, and then manually kill the window. For example
import numpy as np
x = np.arange(1,10)
y = np.arange(1,10)
plt.plot(x,y)
plt.show()
This used to work fine. The window would close and I'd return to the prompt. However, now when I hit the red X to close the window, the window remains open. My command prompt returns and works fine, but the window is stuck and I see the spinning beach ball over it.
(Note that the cursor doesn't appear in the image, but you can see the red X is greyed out because I've hit close but it is stuck open).
It's not until I run the script a second time that the first window closes, but then I'm stuck with a second open plot with the same problem. I'm a bit confused because this only happened recently. Up till now, the window would close fine.
It's not a huge issue, because Python still runs and I can still make new plots, but I'm curious as to why the window would all of a sudden stick open. Any advice?
UPDATE
I solved the problem by switching the interactive backend. Either Qt4Agg or TkAgg as an interactive backend resolves the issue. But the question remains why the macosx and CocoaAgg backends show this behavior.
For a permanent solution (I'd rather not have to switch backends every time I open ipython) you can modify matplotlibrc.
There are various matplotlibrc files that can be changed (i.e. for just a local directory, or globally, etc.). To find the configuration file that's been loaded for your ipython session, use matplotlib.matplotlib_fname(). Here's what I got:
In [1]: import matplotlib
In [2]: matplotlib.matplotlib_fname()
Out[2]: u'/usr/local/lib/python2.7/site-packages/matplotlib/mpl-data/matplotlibrc'
Opening the rc file and changing the line:
backend : macosx
to read:
backend : Qt4Agg
did the trick for me.
I solved the problem by switching the interactive backend. Either Qt4Agg or TkAgg as an interactive backend resolves the issue.
I am trying to run python in XCode. The following simple plotting routine,
import matplotlib.pyplot as pyplot
pyplot.plot((0,1),(1,2))
pyplot.show()
returns no errors. XCode seems pretty happy with what it accomplishes when I hit the run button. But I get no plot window whatsoever, as far as I can tell. Is it hiding somewhere or what? How do I get to see it?
xcode will allow you to see plots if you move your code into an interactive notebook.
create a new file with the .ipynb file extension
copy your code into that file
You should be able not only to see the plots but also any other output your code is trying to send to the UI.
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.
I'm using IPython notebooks to share code and (hopefully) graphics with m collaborators. sadly, I cannot get matplotlib to plot inside the notebook, it always gives me a pop-up window, which I obviously cannot include in notebook pastes or similar.
Here's two minimal examples of how I go about plotting things.
Either with invoking plt.show() , or without it.
this either gives me an external pop-up, or plots nothing.
You need to be using the matplotlib inline backend.
%matplotlib inline