Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.(5)

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/The best way to fix _tkinter.tclerror: no display name and no $display environment variable error
Next
Answered
Mia Bataille
  • 17
Mia Bataille
Asked: May 11, 20222022-05-11T18:47:49+00:00 2022-05-11T18:47:49+00:00In: Programs

The best way to fix _tkinter.tclerror: no display name and no $display environment variable error

  • 17

. Advertisement .

..3..

. Advertisement .

..4..

I get an 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

when I try to run the following code:

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)

How to fix _tkinter.tclerror: no display name and no $display environment variable. Please give me some good ideas.

no display name
  • 3 3 Answers
  • 1k Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

3 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    Juliette Mary
    2022-05-11T18:48:07+00:00Added an answer on May 11, 2022 at 6:48 pm

    It was a problem I was having with Colab. The following code lines helped me to solve it. It should be at the top of your notebook.

    ### CREATE VIRTUAL DISPLAY ###
    !apt-get install -y xvfb # Install X Virtual Frame Buffer
    import os
    os.system('Xvfb :1 -screen 0 1600x1200x16 &') # create virtual display with size 1600x1200 and 16 bit color. Color can be changed to 24 or 8
    os.environ['DISPLAY']=':1.0' # tell X clients to use our virtual DISPLAY :1.0.
    %matplotlib inline
    ### INSTALL GHOSTSCRIPT (Required to display NLTK trees) ###
    !apt install ghostscript python3-tk
    • 7
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Anna
    2022-06-18T23:46:13+00:00Added an answer on June 18, 2022 at 11:46 pm

    Matplotlib defaults to the Xwindows Backend. Matplotlib must be set to disable the Xwindows Backend This code should be added to the beginning of your script ( prior to importing Pyplot).

    import matplotlib
    matplotlib.use('Agg')

    To use non-interactive backend, add .config/matplotlib/matplotlibrc line HTML12

    echo "backend: Agg" > ~/.config/matplotlib/matplotlibrc

    Oder, connect to server using ssh -X remoteMachine command. To use Xwindows You can also export display export DISPLAY=mymachine.com:0.0

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  3. Lou Guichard
    2022-05-11T18:47:55+00:00Added an answer on May 11, 2022 at 6:47 pm

    This is the final answer. It works in 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

    It wasn’t my intention to have it use the Agg' backend. I wanted it to only be used when it would go through Travis CI, for example.

    • 20
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.