In my AWS Ubuntu 14.02 instance I installed the matplotlib dependencies and then via pip I installed matplotlib:
sudo apt-get build-dep python-matplotlib
sudo pip install matplotlib
sudo pip freeze:
matplotlib==1.4.0
in a brand new file plotting1.py I wrote:
!/usr/bin/python
from pylab import *
plot([1,2,3])
show()
after typying chmod +x plotting1.py, I executed the little script:
time python plotting1.py
Traceback (most recent call last):
File "plotting1.py", line 4, in
plot([1,2,3])
File "/usr/local/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 3086, in plot
ax = gca()
File "/usr/local/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 827, in gca
ax = gcf().gca(*_kwargs)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 461, in gcf
return figure()
File "/usr/local/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 434, in figure
*_kwargs)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_qt4ag g.py", line 47, in new_figure_manager
return new_figure_manager_given_figure(num, thisFig)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_qt4ag g.py", line 54, in new_figure_manager_given_figure
canvas = FigureCanvasQTAgg(figure)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_qt4ag g.py", line 72, in init
FigureCanvasQT.init(self, figure)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_qt4.p y", line 68, in init
_create_qApp()
File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_qt5.p y", line 139, in _create_qApp
raise RuntimeError('Invalid DISPLAY variable')
RuntimeError: Invalid DISPLAY variable
real 0m0.404s
user 0m0.284s
What do I have to do now?
Looking forward to your kind help.
Kind regards.
Marco
I had the same problem and I solved it by adding plt.switch_backend('agg') after import matplotlib.pyplot as plt
Reacted by hujilin1229, ChenKuanHung, Samuel Harrold, ZhuFengdaaa, Yuming Wang, Chris, spencerR1992, King Lee, XiZiler, Shenglei and 159 more
Reacted by Le Tuan Anh github, Fu XY, Xin Zhang, lpc, whuhit, xieliy, moshl, Gasoonjia, Aman Dalmia, qxjl1010 and 9 more
Reacted by Le Tuan Anh github, Haleigh, Adam Stelmaszczyk, Xin Zhang, Jie Chen, lpc, Hui Lu, Zaheer, whuhit, xieliy and 19 more
Reacted by Le Tuan Anh github, Salvatore Giorgi, utopia, Xin Zhang, Sanjay Thakur, Vijay Veerabadran, lpc, Zaheer, ehaschia, Rishabh Agrahari and 23 more
import matplotlib matplotlib.use('agg')
Does not work. But
plt.switch_backend('agg')
does the trick.
Reacted by Philip O'Brien, Joshua Smith, Carlos García, Adam Teichert, Mauro Baraldi, Aravindh Kuppusamy, Kseniya Shychko, talpay, Pablo Torre, Si Yi Meng and 20 more
Reacted by César Souza, Hugh Perkins and Akshay Uppal
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt
Demo for data/demo/000456.jpg
Detection took 0.233s for 300 object proposals
Traceback (most recent call last):
File "./tools/demo.py", line 153, in
demo(net, im_name)
File "./tools/demo.py", line 102, in demo
vis_detections(im, cls, dets, thresh=CONF_THRESH)
File "./tools/demo.py", line 51, in vis_detections
fig, ax = plt.subplots(figsize=(12, 12))
File "/home/usr/anaconda2/lib/python2.7/site-packages/matplotlib/pyplot.py", line 1177, in subplots
fig = figure(**fig_kw)
File "/home/usr/anaconda2/lib/python2.7/site-packages/matplotlib/pyplot.py", line 527, in figure
**kwargs)
File "/home/usr/anaconda2/lib/python2.7/site-packages/matplotlib/backends/backend_qt4agg.py", line 46, in new_figure_manager
return new_figure_manager_given_figure(num, thisFig)
File "/home/usr/anaconda2/lib/python2.7/site-packages/matplotlib/backends/backend_qt4agg.py", line 53, in new_figure_manager_given_figure
canvas = FigureCanvasQTAgg(figure)
File "/home/usr/anaconda2/lib/python2.7/site-packages/matplotlib/backends/backend_qt4agg.py", line 76, in init
FigureCanvasQT.init(self, figure)
File "/home/usr/anaconda2/lib/python2.7/site-packages/matplotlib/backends/backend_qt4.py", line 68, in init
_create_qApp()
File "/home/usr/anaconda2/lib/python2.7/site-packages/matplotlib/backends/backend_qt5.py", line 138, in _create_qApp
raise RuntimeError('Invalid DISPLAY variable')
RuntimeError: Invalid DISPLAY variable
I have modified as you suggested, by replacing plt with plt.switch_backend('agg')
I have the following error:
Demo for data/demo/000456.jpg
Detection took 0.268s for 300 object proposals
Traceback (most recent call last):
File "./tools/demo2.py", line 153, in
demo(net, im_name)
File "./tools/demo2.py", line 102, in demo
vis_detections(im, cls, dets, thresh=CONF_THRESH)
File "./tools/demo2.py", line 51, in vis_detections
fig, ax = plt.switch_backend('agg').subplots(figsize=(12, 12))
AttributeError: 'NoneType' object has no attribute 'subplots'
For others having this issue: for me it was solved by putting the
import matplotlib
matplotlib.use('agg')
all the way at the top of the file it was used in. Initially I though "hmm, it should be fine as long as I put it above all other matplotlib stuff", but nope! It had to be all the way on the top for it to work.
switch_backend() probably won't work (although we are working on
improvements to make it possible under certain situations).
It would be hard to figure out your problem without more information. What
OS? Which version of matplotlib? And how did you install matplotlib?
If the server you are logging into does not have xserver running in some
form or another, then, no you can't get a GUI window since the GUI window
would have to come from the server you are logging into. But, usually,
xserver is available on most systems, and usually, doing `ssh -X ...` (or
sometimes, ssh -Y ...` is sufficient to make this work when logging into a
remote server. I should note that I have experienced issues with my CentOS7
machine, and I suspect it has to do with some weird interaction with
SELinux and plymouth. But in general, it should work if you include "-X"
option for your ssh calls.
Note, if you are making multiple ssh hops, each one of those hops will need
the -X option invoked.
Activity
cimarronm commented on Sep 4, 2014
Do you have the X server running? What is your DISPLAY environment variable set to (echo $DISPLAY)?
marcoippolito commented on Sep 4, 2014
Hi,
thanks for helping!!!!
echo $DISPLAY
so it say nothing
What do I have to do to make X server running in my AWS Ubuntu 12.04 instance?
I rebooted but still nothing:
echo $DISPLAY
Do you know how to set the DISPLAY environment variable to disp?
marcoippolito commented on Sep 4, 2014
Solved. I cannot visualize graphs on AWS server.
so I have to save them in files and download them locally.
Thanks for your kind help.
Kind regards.
Marco
LinZichuan commented on Mar 13, 2016
I had the same problem and I solved it by adding plt.switch_backend('agg') after import matplotlib.pyplot as plt
amberkaka commented on Apr 22, 2016
@LinZichuan I encoutered this problem too and your solution works. Thanks for your sharing!
jenshnielsen commented on Apr 23, 2016
For the record it's better to chose a backend before importing pyplot i.e.
aluenkinglee commented on Jan 3, 2017
@LinZichuan Great! It works
bzhr commented on Jan 5, 2017
For me on AWS ec2 instance
import matplotlib matplotlib.use('agg')
Does not work. But
plt.switch_backend('agg')
does the trick.
hana9090 commented on Apr 1, 2017
@jenshnielsen
I have this error, although I add
import matplotlib matplotlib.use('agg') import matplotlib.pyplot as plthana9090 commented on Apr 1, 2017
@bzhr
I have modified as you suggested, by replacing plt with plt.switch_backend('agg')
I have the following error:
tacaswell commented on Apr 2, 2017
@hana9090 That is not how the library works. plt.switch_backend() changes some global state which affects how future calls to plt.subplots() works.
teuneboon commented on Jul 24, 2017
For others having this issue: for me it was solved by putting the
import matplotlib matplotlib.use('agg')all the way at the top of the file it was used in. Initially I though "hmm, it should be fine as long as I put it above all other matplotlib stuff", but nope! It had to be all the way on the top for it to work.
tacaswell commented on Jul 24, 2017
@teuneboon It must be above anything that imports pyplot.
Use switch_backend instead, see matplotlib/matplotlib#3466
3 remaining items
WeatherGod commented on Nov 17, 2017
sulth commented on Nov 18, 2017
Hi Thanks !i got it.it was due to agg (non interactive visualisation env).replaced it with qt4agg (interactive)and it worked
Use plt.switch_backend
XiangyunHuang commented on Apr 16, 2018
That works for me in R Markdown
Naman-ntc commented on May 22, 2018
Is there any other method where matplotlib plots and shows over ssh or manually saving is the only option??
WeatherGod commented on May 22, 2018
Declare a backend for matplotlib
Declare a backend for matplotlib (#171)
kapil-varshney commented on Aug 6, 2018
import matplotlib matplotlib.use('agg') import matplotlib.pyplot as plt^ Solved the problem for me. Thanks @jenshnielsen .
Use plt.switch_backend
debovis commented on May 13, 2019
Switching to 'agg' does solve the issue. Although I am back stuck at this issue: #12074
Working on sumsims, incrementing version.
Use plt.switch_backend
Use plt.switch_backend
Use plt.switch_backend