I am trying to run a python script on my linux server and make and save some plots. I have installed ipython
and pylab
and matplotlib
but then when I run my script I get this error:
Traceback (most recent call last):
File "/root/dining_hall_graph.py", line 14, in <module>
from pylab import *
File "/usr/lib64/python2.7/site-packages/pylab.py", line 1, in <module>
from matplotlib.pylab import *
File "/usr/lib64/python2.7/site-packages/matplotlib/pylab.py", line 265, in <module>
from matplotlib.pyplot import *
File "/usr/lib64/python2.7/site-packages/matplotlib/pyplot.py", line 97, in <module>
_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
File "/usr/lib64/python2.7/site-packages/matplotlib/backends/__init__.py", line 25, in pylab_setup
globals(),locals(),[backend_name])
File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_gtkagg.py", line 10, in <module>
from matplotlib.backends.backend_gtk import gtk, FigureManagerGTK, FigureCanvasGTK,\
File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_gtk.py", line 13, in <module>
import gtk; gdk = gtk.gdk
File "/usr/lib64/python2.7/site-packages/gtk-2.0/gtk/__init__.py", line 64, in <module>
_init()
File "/usr/lib64/python2.7/site-packages/gtk-2.0/gtk/__init__.py", line 52, in _init
_gtk.init_check()
RuntimeError: could not open display
On this line from pylab import *
How can I plot and save graphs in my python script that I am running on my linux server?
Thanks
We can simply save plots generated from Matplotlib using savefig() and imsave() methods. If we are in interactive mode, the plot might get displayed. To avoid the display of plot we use close() and ioff() methods.
Plotting from an IPython shell draw() . Using plt. show() in Matplotlib mode is not required.
Matplotlib is not a built-in module (it doesn't come with the default python installation) in Python, you need to install it explicitly using the pip installer and then use it. If you looking at how to install pip or if you are getting an error installing pip checkout pip: command not found to resolve the issue.
The last, Agg, is a non-interactive backend that can only write to files. It is used on Linux, if Matplotlib cannot connect to either an X display or a Wayland display.
You should almost never use from pylab import *
.
Import what you need from numpy
, scipy
and matplotlib
.
On machines without display, you need to use the agg
backend:
You can achieve this by specifying the environmental variable MPLBACKEND
$ MPLBACKEND=Agg python plot.py
Or by importing matplotlib
before importing matplotlib.pyplot
:
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
plt.plot([1, 2, 1])
plt.savefig('test.pdf')
To make this the default, you can create a file called matplotlibrc
in the current directory or in $HOME/.config/matplotlib
with the following content:
backend: Agg
Or you could enable X-Forwarding, so the plots will pop up on your host machine:
ssh -X user@server
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With