1

This code connects to MySQL and fetches a dataset which I read using pandas. Once the dataset is in a pandas dataframe, I need to plot it. But that throws an error. Here's the snipper

#!/usr/bin/python

import MySQLdb as mdb
import pandas as pd
import matplotlib.pyplot as plt

con = mdb.connect('hostname', 'username', 'password', 'database');

with con:
    cur = con.cursor()
    cur.execute("select month, post, comment, reply, dm, review")
    rows = cur.fetchall()
    df = pd.DataFrame( [[ij for ij in i] for i in rows] )
    df.rename(columns={0: 'Month', 1: 'Post', 2: 'Comment', 3: 'Reply', 4: 'DM', 5: 'Review'}, inplace=True);
    print(df.head(20))

df=df.set_index('Month')
df=df.astype(float)
df.plot(subplots=True)
plt.show()

Traceback (most recent call last):
  File "random-exmaple.py", line 7, in <module>
    plt.plot(x, y, "o")
  File "/usr/local/lib/python2.7/site-packages/matplotlib/pyplot.py", line 3307, in plot
    ax = gca()
  File "/usr/local/lib/python2.7/site-packages/matplotlib/pyplot.py", line 950, in gca
    return gcf().gca(**kwargs)
  File "/usr/local/lib/python2.7/site-packages/matplotlib/pyplot.py", line 586, in gcf
    return figure()
  File "/usr/local/lib/python2.7/site-packages/matplotlib/pyplot.py", line 535, in figure
    **kwargs)
  File "/usr/local/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.py", line 81, in new_figure_manager
    return new_figure_manager_given_figure(num, figure)
  File "/usr/local/lib/python2.7/site-

packages/matplotlib/backends/backend_tkagg.py", line 89, in new_figure_manager_given_figure
    window = Tk.Tk()
  File "/usr/local/lib/python2.7/lib-tk/Tkinter.py", line 1745, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable

How to fix this?

Sorry for the duplicate - Issue with tkinter, python and seaborn: _tkinter.TclError: no display name and no $DISPLAY environment variable

Community
  • 1
  • 1
MontyPython
  • 2,906
  • 11
  • 37
  • 58

1 Answers1

0

This kind of error appears when we try to run GUI applications or plot something on remote machine, which has not connected display device. In this case, you can save your plots on remote machines and copy them on local machine via scp. You can do it this way:

Save plots:

ax = df.plot(subplots=True)
fig = ax.get_figure()
fig.savefig('plots_name.png')

Copy images from remote machine to local directory:

scp {remote_username}@{remote_host}:{path_to_image}  {path_to_local_directory}

where {remote_host} is the IP address or domain name that you are trying to connect to, {remote_user} is your username on remote machine, {path_to_image} is path on remote machine to image you want to copy (use pwd command to find it) and {path_to_local_directory} is path to local directory, where you want your plot will appear.

EDIT:

For newcomers. It's more complicated solution. Author of this question has provided simple solution to solve this problem. Check this: Issue with tkinter, python and seaborn: _tkinter.TclError: no display name and no $DISPLAY environment variable

Community
  • 1
  • 1
Eduard Ilyasov
  • 3,268
  • 2
  • 20
  • 18