Python is known as a wide language with an infinite number of projects and programs along with a variety of libraries, logs, functions, etc. It has given programmers the liberty to use Python functions to design websites and web applications just the way they want. When you use Python, it is quite normal to have encountered errors. You may experience the error “_tkinter.tclerror: no display name and no $display environment variable error”, which seems frustrating.
No need to lose hope as there is no such error that can’t be fixed. You get the error warning when logging in to a remote machine and trying to use data to plot graphs or charts. Have a look at how the error occurs
How the error pops up
When you try to run the Python script like this:
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)
You end up with the error warning. Have look at an error that shows up
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
You may have installed matplotib 1.5.1 on your system and have used it in your code, and that’s exactly when you get the error. Now that you have seen how you get yourself into the error, let’s check out how to solve it
Solutions to Handle “_tkinter.tclerror: no display name and no $display environment variable error”
A few effective yet simple solutions to tackle the error warning are as follow
Solution 1 – Implement the ‘Agg’ backend
To remove the error, you need to implement the Agg backend. You can simply follow the code
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
Solution 2 – Add the code to you.py file on the top
In order to get rid of the error warning, you need to add a code in your.py file on the top. Have a look at the code
import matplotlib
matplotlib.use(‘Agg’)
It signifies that matplotlib should refrain from using the backend of Xwindows.
Conclusion
In this post, we have discussed the best possible solutions to fix “_tkinter.tclerror: no display name and no $display environment variable error”.
I hope you can solve the problem!
Leave a message if you need assistance. Your feedback is highly appreciated!
Reference Source: https://syntaxfix.com/question/2984/tkinter-tclerror-no-display-name-and-no-display-environment-variable