I am new to python and trying to explore graphs, could you please help me understand if I can plot graphs on the console using matplotlib on a linux system that has no XSERVER running?
Thanks.
You can use matplotlib with no X server by setting the backend to Agg, PS, PDF or SVG, Cairo or GDK (depending on what kind of file you wish to create). You can set the backend in your matplotlibrc file, which, depending on your installation, may be in a directory such as ~/ or ~/.matplotlib or ~/.config/matplotlib/.
Alternatively, you can set the backend in the script itself.
Be sure to set the backend first, before importing other modules such as pyplot:
import matplotlib
matplotlib.use("Agg")
See this SO question for examples.
Related
I'm trying to make an .exe file from .py and I'm having an issue with matplotlib library. When I run my script in VS Code plt.show() works just fine. But when I use pyinstaller to make an .exe and run the .exe file, it gives me a following warning: "UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure." Importing TkAgg directly with "import matplotlib.backends.backend_tkagg" doesn't solve the problem either.
Is it possible to fix that?
I ran into the same issue.
I found this article, which may be helpful:
"UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure." when plotting figure with pyplot on Pycharm
However, the simple fix in my case was to just explicitly import the backend module(s):
import matplotlib.backends.backend_tkagg
import matplotlib.backends.backend_wxagg
Pyinstaller does some relatively sophisticated analysis and Matplotlib itself has some relatively sophisticated code to be able to use multiple GUI backends. I think the overall problem is that Pyinstaller isn't seeing that any particular backend is actually being used and so doesn't end up bundling any backend module.
Explicitly making the import makes it obvious to Pyinstaller that you want that module (importing it is effectively using it). (Maybe you only need the one for 'tkagg' - I happen to be working with a WX GUI, so I included both imports - it works.)
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.
Is it possible to use matplotlib plot or a plotting tool similar to this, to save a plot in a python2 application to a file when there is no display on the server I'm working on?
Context:
I am sshing onto a server cluster running New Debian to run a series of large optimisations in Python. I'd really like to be able to save those optimisations as plots (eg. a file such as "opt-100.png") as well as the raw data. I am unable to do so as neither pylab nor matplotlib.pyplot can be imported because there's no display (ie. "RuntimeError: could not open display" is thrown by python when I try and import them).
I realise I am able to save the raw data and then scp that onto my local machine but due to the current configuration of my code that would be a lot of work!!
The backend for Matplotlib has to be set to use software rendering instead of hardware rendering. Try this before any matplotlib or pylab imports:
import matplotlib
matplotlib.use('Agg')
I am trying to display the x y z coordinate of an image in matplotlib. the example code work perfectly well on the global python installation: As I move the cursor the x,y,z values get updated instantaneously. However, when I run the example code on a python virtual environment, I would click on the images several times for the coordinate to show in the first place, then when I click on different positions it would update for some. After few clicks, the coordinates will no longer update.
I don't know how to debug this.
This is likely to be a problem with the macosx backend for matplotlib. Switch to using an alternative backend for matplotlib (e.g. use qt4 instead of 'macosx'). For details of how to switch backend and what exactly that means - see the docs here. Note that you might have to install the backend first - e.g. pyqt to use the qt4agg backend as I'm suggesting here.
In summary - the backend deals with the output from matplotlib and matplotlib can target different output formats. These can be gui display output formats (for instance wx, qt4 and so on), or file outputs (for instance pdf). These are known as interactive and non-interactive backends respectively.
To change backend either do
import matplotlib
matplotlib.use('qt4agg')
in code, or - if you want to change for every time you start matplotlib - edit your matplotlibrc file setting the backend attribute e.g.
backend: Qt4Agg
N.B. I was alerted by a comment that since posting this answer, matplotlib docs now refer to this issue and suggest a workaround, although the commenter noted that the solution offered in this answer (switch to Qt backend) worked for them where the official docs workaround was not possible for them.
What finally worked for me was to make a local matplotlibrc file containing the directive: backend: TkAgg.
# Within working directory where running python
vim matplotlibrc
# new file via vim, Nano, whatever
backend: TkAgg
This was useful:
import matplotlib
print matplotlib.rcParams['backend']
Other Notes:
I had also installed pyqt using Homebrew (brew install pyqt) and copied (could just move it) into $MyVirtEnv/lib/python2.7/site-packages/ from /usr/local/lib/python2.7/site-packages/PyQt4
But when I
import matplotlib
matplotlib.use('qt4Agg')
import librosa
"Matplotlib qt-based backends require an external PyQt4, PyQt5,\n"
ImportError: Matplotlib qt-based backends require an external PyQt4, PyQt5,
or PySide package to be installed, but it was not found.
Had also tried
pip install pyside
Successfully installed pyside-1.2.4
Same error
>>> import pyside
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named pyside
Also got the following error at some point when trying to reimport matplotlib (i think).
from . import cache
ImportError: cannot import name cache
As pointed out in Matplotlib virtualenv FAQ, vext is the solution here.
It allows to use the system-wide pyqt5 for instance.
This is necessary until PyQt5 setup knows about virtualenvs:
pip3 install vext.pyqt5
Just add plt.show() at the end and the problem was solved for me.