gpt4 book ai didi

python - Matplotlib 图表未显示

转载 作者:行者123 更新时间:2023-12-01 02:53:01 26 4
gpt4 key购买 nike

我的项目要求我在 Windows 系统上使用 putty 和 Connectify 热点打开 Raspberry Pi 的终端来绘制和显示图形。然而,该图只能显示在我的 Raspberry Pi 显示器上,而不能显示在我的 Window 显示器上。这是我使用的代码:

import pymysql
import matplotlib
from matplotlib import pyplot as plt
import numpy as np
import pandas as pd

conn = pymysql.connect(host="localhost", user="root", passwd="123456", db="XXX")

cur = conn.cursor()

query = """
SELECT data,time
FROM sensordata
WHERE time >= "2017-05-21"
AND time < "2017-05-23"
"""


cur.execute(query)

data = cur.fetchall()

cur.close()
conn.close()

time,data= zip(*data)
plt.plot(data,time)

plt.title("XXX ")
plt.xlabel("Time & Date ")
plt.ylabel("Strength")
fig = plt.gcf()

fig.set_size_inches (55,27.5)
plt.grid(True)
plt.draw()
fig.savefig('test.png' ,dpi=100)
plt.show()

当我尝试在 putty 终端上运行它时收到的错误是:

Traceback (most recent call last):
File "matplot2.py", line 27, in <module>
plt.plot(data,time)
File "/usr/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 3092, in plot
ax = gca()
File "/usr/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 828, in gca
ax = gcf().gca(**kwargs)
File "/usr/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 462, in gcf
return figure()
File "/usr/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 435, in figure
**kwargs)
File "/usr/lib/python2.7/dist-packages/matplotlib/backends/backend_tkagg.py", line 81, in new_figure_manager
return new_figure_manager_given_figure(num, figure)
File "/usr/lib/python2.7/dist-packages/matplotlib/backends/backend_tkagg.py", line 89, in new_figure_manager_given_figure
window = Tk.Tk()
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1813, in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable

我尝试使用我读过的一些解决方案中的 matplotlib.use('Agg')matplotlib.use('TKAgg') 但它没有解决我的问题。希望有人能够解决我的问题,以便我可以在我的 Window 显示器上显示图形...提前致谢

最佳答案

错误“_tkinter.TclError:无显示名称且无 $DISPLAY 环境变量”表明您的 X11 服务器存在问题。

我不知道您从哪个主机在 Raspberry 上进行 ssh,但您必须在 Windows 上运行 X-Server(例如 VcXsrv )。然后确保允许 X11 转发,例如ssh -X 甚至 ssh -Y。使用 putty 时,请确保在 Connection -> SSH -> X11 下设置启用 X11 转发

如果一切都设置完毕,运行 matplotlib 条形图演示(请参阅注释)应如下所示:

enter image description here(没有图像,因为有人出于某种原因否决了答案)

如果问题仍然存在,您可以尝试使用 MobaXTerm(在 Windows 上),它附带 X Server,并且应该可以开箱即用。

如果您正在运行 VcXSrv,对我来说,我必须通过在 shell 中运行以下代码来导出 $DISPLAY 变量:export DISPLAY=localhost:0.0

关于python - Matplotlib 图表未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44517289/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com