【问题标题】:Unable to Plot graph using pyplot, pandas: _tkinter.TclError: no display name and no $DISPLAY environment variable无法使用 pyplot、pandas 绘制图形:_tkinter.TclError: no display name and no $DISPLAY environment variable
【发布时间】:2017-06-16 16:45:32
【问题描述】:

此代码连接到 MySQL 并获取我使用 pandas 读取的数据集。一旦数据集在熊猫数据框中,我需要绘制它。但这会引发错误。这是狙击手

#!/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

如何解决这个问题?

抱歉重复 - Issue with tkinter, python and seaborn: _tkinter.TclError: no display name and no $DISPLAY environment variable

【问题讨论】:

  • 您是尝试在本地还是通过 ssh 在远程机器上运行此代码?
  • @EduardIlyasov - 通过 ssh 远程机器。

标签: python mysql pandas matplotlib


【解决方案1】:

当我们尝试在没有连接显示设备的远程机器上运行 GUI 应用程序或绘图时会出现这种错误。在这种情况下,您可以将绘图保存在远程机器上并通过 scp 将它们复制到本地机器上。你可以这样做:

保存地块:

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

将图像从远程机器复制到本地目录:

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

{remote_host} 是您尝试连接的 IP 地址或域名,{remote_user} 是您在远程计算机上的用户名,{path_to_image} 是远程计算机上要复制的映像的路径(使用 @987654327 @命令找到它)和{path_to_local_directory}是本地目录的路径,你希望你的情节会出现在哪里。

编辑:

对于新手。这是更复杂的解决方案。这个问题的作者提供了简单的解决方案来解决这个问题。检查这个:Issue with tkinter, python and seaborn: _tkinter.TclError: no display name and no $DISPLAY environment variable

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-04
    • 1970-01-01
    • 2017-05-12
    相关资源
    最近更新 更多