• Register
0 votes
1.2k views

Problem :

I am learning Python. I have written a basic python script and trying to run the basic python script on the server:

import matplotlib.pyplot as plot
import numpy as nump

x = nump.random.randn(80)
y = nump.random.randn(80)

plot.scatter(x, y, s=30)

out_png = 'path/to/store/out_file.png'
plot.savefig(out_png, dpi=180)

I am trying to use a command python pyexample.py on this server which also has the matplotlib 1.5.1 installed on it but it fails with below 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

Why am I facing above error? How can I fix it?

8 4 2
2,300 points

1 Answer

0 votes

Solution :

I had faced such issue earlier. To resolve the issue I just used below code at a very beginning of your python script. So it should be able to run smoothly on the different environments.

import os
import matplotlib as matpl
if os.environ.get('DISPLAY','') == '':
    print('Currently no display found. Using the non-interactive Agg backend')
    matpl.use('Agg')
import matplotlib.pyplot as plot

 It will fix your issue for sure.

5 2 1
4,980 points

Related questions

1 vote
1 answer 302 views
302 views
Problem: I installed tkinter and I wrote this code to check if this works or not: import tkinter as tink a = tink.Tink() a.title('Total Seconds') button = tink.Button(a, text='Welcome', width=40, command=a.destroy) button.pack() a.mainloop() But after running this ... ", line 2, in <module> a = tink.Tink() AttributeError: module 'tkinter' has no attribute 'Tink' Can anyone help me to solve this?
asked Apr 29 samhaz 5k points
0 votes
1 answer 1.5K views
1.5K views
Problem : The import matplotlib.pyplot as plt is is giving me the error with python2.7, anaconda3. The error shown is as below: ImportError: libGL.so.1: cannot open shared object file: No such file or directory The Detailed Error report is as below : Traceback ... <module> from PyQt5 import QtCore, QtGui, QtWidgets ImportError: libGL.so.1: cannot open shared object file: No such file or directory
asked Dec 18, 2019 alecxe 7.5k points
0 votes
1 answer 265 views
265 views
Problem: I have recently installed opencv with all the dependencies. After the successful installation I have tried to import matplotlib for the very simple example. I have started facing below error when I only tried to install matplotlib via the pip with pip install ... linux-gnu.so' Consider using the `--user` option or check the permissions. How can I install matplotlib without any error?
asked Aug 26 Raphael Pacheco 4.9k points
0 votes
1 answer 40 views
40 views
Problem : I am new to python. I have tried to install below softwares: Python 2.5.4, Numpy 1.5.0 win32, Matplotlib 1.0.0 win32, pywin32 218. After doing so much of hard work I am still unable to plot the graphs in Python. I am facing ... > from matplotlib._path import affine_transform ImportError: DLL load failed: The specified module could not be found. Can somebody help me in fixing this error?
asked Feb 22 mphil 2.3k points
0 votes
1 answer 216 views
216 views
Problem : I have just upgraded to a latest stable release of matplotlib (1.5.1) and everytime I import the matplotlib I get below message: /usr/local/lib/python2.7/dist-packages/matplotlib/font_manager.py:273: UserWarning: Matplotlib is building the font cache using fc ... always stalls for the few seconds. Is this a expected behavior? Was it a same also before, but just without a printed message?
asked Jan 24 jwilliam 3.9k points