I am running a simple python script in the server:
import matplotlib.pyplot as plt import numpy as np x = np.random.randn(60) y = np.random.randn(60) plt.scatter(x, y, s=20) out_png = 'path/to/store/out_file.png' plt.savefig(out_png, dpi=150)
I try to use the command
python example.py in this server which has matplotlib 1.5.1 installed it fails with the error:
Traceback (most recent call last): File "example.py", line 7, in <module> plt.scatter(x, y, s=20) File "/home/USER/.virtualenvs/nnet/lib/python2.7/site-packages/matplotlib/pyplot.py", line 3241, in scatter ax = gca() File "/home/USER/.virtualenvs/nnet/lib/python2.7/site-packages/matplotlib/pyplot.py", line 928, in gca return gcf().gca(**kwargs) File "/home/USER/.virtualenvs/nnet/lib/python2.7/site-packages/matplotlib/pyplot.py", line 578, in gcf return figure() File "/home/USER/.virtualenvs/nnet/lib/python2.7/site-packages/matplotlib/pyplot.py", line 527, in figure **kwargs) File "/home/USER/.virtualenvs/nnet/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.py", line 84, in new_figure_manager return new_figure_manager_given_figure(num, figure) File "/home/USER/.virtualenvs/nnet/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.py", line 92, in new_figure_manager_given_figure window = Tk.Tk() File "/usr/local/lib/python2.7/lib-tk/Tkinter.py", line 1810, in __init__ self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use) _tkinter.TclError: no display name and no $DISPLAY environment variable
What is happening here?
Matplotlib chooses Xwindows backend by default. You need to set matplotlib to not use the Xwindows backend.
Add this code to the start of your script (before importing pyplot) and try again:
import matplotlib matplotlib.use('Agg')
Or add to
backend: Agg to use non-interactive backend.
echo "backend: Agg" > ~/.config/matplotlib/matplotlibrc
Or when connect to server use
ssh -X remoteMachine command to use Xwindows.
Also you may try to export display:
You can solve it by adding these two lines in the VERY beginning of your .py script.
import matplotlib matplotlib.use('Agg')
PS: The error will still exists if these two lines are not added in the very beginning of the source code.
To add up on the answer, I used this at the beginning of the needed script. So it runs smoothly on different environments.
import os import matplotlib as mpl if os.environ.get('DISPLAY','') == '': print('no display found. Using non-interactive Agg backend') mpl.use('Agg') import matplotlib.pyplot as plt
Because I didn’t want it to be alsways using the
'Agg' backend, only when it would go through Travis CI for example.
I had this same issue trying to run a simple tkinter app remotely on a Raspberry Pi. In my case I did want to display the tkinter GUI on the pi display, but I want to be able to execute it over SSH from my host machine. I was also not using matplotlib, so that wasn’t the cause of my issue. I was able to resolve the issue by setting the DISPLAY environment variable as the error suggests with the command:
A good explanation of what the display environment variable is doing and why the syntax is so odd can be found here: https://askubuntu.com/questions/432255/what-is-display-environment-variable
Another solution is to install Xvfb, and export your display to it. ie:
disp=:8 screen=0 geom=640x480x24 exec Xvfb $disp -screen $screen $geom 2>/tmp/Xvfb.log &
$ export DISPLAY=:8
In order to see images, plots and anything displayed on windows on your remote machine you need to connect to it like this:
ssh -X [email protected]
That way you enable the access to the X server. The X server is a program in the X Window System that runs on local machines (i.e., the computers used directly by users) and handles all access to the graphics cards, display screens and input devices (typically a keyboard and mouse) on those computers.
More info here.
I also met this problem while using Xshell to connect Linux server.
After seaching for methods, I find Xming + Xshell to solve image imshow problem with matplotlib.
If solutions aboved can’t solve your problem, just try to download Xming under the condition you’re using Xshell. Then set the attribute in Xshell, SSH->tunnel->X11transfer->choose X DISPLAY localhost:0.0
I want to add an answer here that noone has explicitly stated with implementation.
This is a great resource to refer to for this failure: https://matplotlib.org/faq/usage_faq.html
In my case, using
matplotlib.use did not work because it was somehow already set somewhere else. However, I was able to get beyond the error by defining an environment variable:
This takes care of the issue.
My error was in a CircleCI flow specifically, and this resolved the failing tests. One wierd thing was, my tests would pass when run using
pytest, however would fail when using
parallelism along with
circleci tests split feature. However, declaring this env variable resolved the issue.