I know this question or a related one has been asked many times, but I still have not found a solution: I ssh -X into a linux machine, where I run a conda installation of ipython. After importing matplotlib, I generate a plot (e.g., plt.plot(1,1)) and I want to show the plot in a window, which is forwarded to my local machine with plt.show(). This used to work, but suddenly no longer does: plt.show() does nothing. I guess this is related to a conda update, which I did yesterday, since a conda list --revisions does not show any suspicious recent changes in my environment.
From similar posts, I guess this is related to the matplotlib backend. The default backend 'Agg' generates png's via plt.savefig, but does not produce a plot in its own window, in which I can zoom. I have not managed to change the backend, though:
In [7]: import tkinter
In [8]: import matplotlib
In [9]: matplotlib.use('TkAgg')
In [10]: import matplotlib.pyplot as plt
gives
ImportError: Cannot load backend 'TkAgg' which requires the 'tk' interactive framework, as 'headless' is currently running
I have tried various similar things but with no success. Any suggestions?
Thank you!
Related
I am using PyCharm on my MacBook to code and now I wanted to make a simple plot. When I run the file via the usual 'Run' command (do not know what it is called), it nicely shows my plots, but when I run the file in the Python console (which I find more convenient because you can access your variables) it does not show anything. On the other hand, when I just type it in the Python console afterwards, it does work.
I have read some things about backends and other 'solutions' as I am apparently not the only one with this issue. Mine says macosx and gives the command: "Backend MacOSX is interactive backend. Turning interactive mode on." after running the file in the Python console. I tried changing the backend:
import matplotlib
# matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
but that does not work (no plot pops up). And yes, I use plt.show() after my plotting section :)
I also tried with 'QtAgg' but then I get: "ImportError: Failed to import any qt binding"
Now I am completely new to this backends stuff (I think it has to do with this), so I could really use some clear directions on how I can solve this issue.
Thanks in advance!
I am not sure we can solve this bug with some adjustment. I think you need to fresh start. I suggest you to start a new clean venv and install a new matplotlib there.
When trying to import pyplot from matplotlib, I get this error (python 3.6). It works fine on my other computer, which has python 3.5, but I don't think it changed much.
I've tried both:
from matplotlib import pyplot as plt
and
import matplotlib.pyplot as plt
It's really annoying me that I can't figure this out.
matplotlib targets many different use cases and output formats. Some people use matplotlib interactively from the python shell and have plotting windows pop up when they type commands. Some people embed matplotlib into graphical user interfaces like wxpython or pygtk to build rich applications. Others use matplotlib in batch scripts to generate postscript images from some numerical simulations, and still others in web application servers to dynamically serve up graphs.
To support all of these use cases, matplotlib can target different outputs, and each of these capabilities is called a backend; the “frontend” is the user facing code, i.e., the plotting code, whereas the “backend” does all the hard work behind-the-scenes to make the figure. There are two types of backends: user interface backends (for use in pygtk, wxpython, tkinter, qt4, or macosx; also referred to as “interactive backends”) and hardcopy backends to make image files (PNG, SVG, PDF, PS; also referred to as “non-interactive backends”).
In yout case you have to choose backend "Agg" to use pyplot.
Solution:
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
reference:
https://matplotlib.org/tutorials/introductory/usage.html#what-is-a-backend
https://github.com/matplotlib/matplotlib/issues/9954
Let me guess:
1:Maybe you have not properly installed matplotlib, you can try this:
pip3.6 install --upgrade matplotlib
Or just deleted the package and reinstall
2:Check whether you have use the same config environment and execution environment:
In some cases, you have installed packages for your local python interpreter, but you actually run your program on python virtual env. Maybe you have not properly installed the package on virtual env.
I'm new to Python and matplotlib. A simple script I wrote is crashing and I was able to reproduce the crash with the following code:
import matplotlib.pyplot as plt
plt.figure(1)
plt.figure(2)
#plt.show()
The error is python.exe has stopped working. If I uncomment the plt.show(), it still crashes depending on the order I close the plots (no crash if 2 is closed first, crash if 1 is closed first). I'm using Windows 7, Python 3.4, and I installed the individual modules from www.lfd.uci.edu/~gohlke/pythonlibs/. Do I have something configured incorrectly or a misunderstanding of how to use matplotlib?
You need to set the TkAgg backend explicitly. With the following code, the problem is resolved.
import matplotlib
matplotlib.use("TkAgg")
from matplotlib import pyplot as plt
Note that setting the TkAgg backend after importing pyplot does not work either; it crashes too. You need to set it before importing pyplot.
I was having this issue, I thought it was some line in my code causing the bug, but in fact the very act of importing matplotlib.pyplot was killing my program. I solved it by first running it in verbose mode:
python -v [programname].py
This shows the last action the importer does before crashing. For me, the last line of this was:
import 'PyQt5' # <_frozen_importlib_external.SourceFileLoader object at 0x000001F8EC9C0908>
This suggested to me that the dependent library PyQt5 was causing issues, so I ran pip install PyQt5, and magically everything started working.
This could be issue with python 3.x
I have tried with python 2.7 on my windows machine and it works perfectly fine!
You can either downgrade your python to 2.7 or if you feel its too late to do why dont you give it a try to call close()
Import matplotlib
matplotlib.use('wxAgg')
Import matplotlib.pyplot as plt
# your scripts
plt.close('all')
I had a similar issue in OSX when I updated to Python 3.4. IDLE was also crashing and there was a warning telling me the version was unstable.
I solved it by following the prompts and updating the version of Tcl/Tk (8.5.9) - http://www.python.org/download/mac/tcltk .
For macOS, just make sure that
~/.matplotlib/matplotlibrc contains:
backend: MacOSX
You don't need the other backends unless you specifically want them. Alternatively, perhaps you can do:
import matplotlib
matplotlib.use("MacOSX")
though I have not tested that.
I want buttons and other interactive matplotlib objects to appear from within my ipython notebook.
Here is what I've done:
Installed http://datasciencetoolbox.org, it is a vagrant box with ipython installed and version 1.3.1 of matplotlib.
I needed to upgrade matplotlib to the latest version, because it has this capability to do inline interactive plots. What's new in Matplotlib 1.4.1
I needed to run sudo apt-get install pkg-config and
sudo pip install matplotlib --upgradein order to get that going.
Then, in order to produce the nice (i.e. error-free) screenshot below, I went into the .ipython/dst-profile/ipython_notebook_config.py file and erased the line about IPKernelApp.pylab='inline' to be able to run the matplotlib.use('nbagg') command.
Then I was able to create the screenshot below. However, things still look poor. Those buttons are not buttons. That is an image of buttons. Please advise on how to make those buttons come to life!
Oh... and check this out if this helps you help me.
Thanks!
Basically you are facing two issues
the %pylab inlinecall overrides the matplotlib.use('nbagg')call, to use the inline backend instead of the nbagg backend which you are actually wanting. If you use a recent version of IPython (2.3) you can directly use %matplotlib nbagg (or %matplotlib notebook) to load the nbagg backend instead of your %pylabcall.
once you enabled the nbagg backend you will need to explicitly show it, ie. add a plt.show() call at the end of your script -> Update: with IPython 2.3.1 this is no longer needed (thanks #tcaswell for the hint)
With this you get the interactive matplotlib experience embedded in the IPython notebook. However, a quick try of your code does't yield to the desired result. The Button reacts and the callback is executed but the print call doesn't show anything. Anyway, to see that it's working try the following simple example (requires IPython 2.3):
%matplotlib nbagg
from matplotlib.widgets import Button
import matplotlib.pyplot as plt
def callback(event):
plt.text(event.xdata, event.ydata, 'clicked')
f,a = plt.subplots(1)
b1 = Button(a,'Button1')
b1.on_clicked(callback)
plt.show()
Btw. it is highly recommended to use %matplotlib instead of %pylab as later leads to some side effects, see here.
Knowing that this is a question, 100times asked, but no solution helps me.
Trying to make a simple plot:
import matplotlib.pyplot as p
p.plot(range(20), range(20))
p.show()
I used several backends in ~/.matplotlib/matplotlibrc, such as GTK, GTKAgg, Qt4Agg, gdk, agg, svg, kairo,...), by adding the line
backend: <backend>
But nothing happens when I try to plot. No error message, simply nothing. I used both the standard console python2.7 and the IPython console.
I'm using CentOS with Gnome Desktop. Matplotlib is 1.2.0.
Do you have any ideas?
NOTE: I realized that it is a problem only as non-root user.