gpt4 book ai didi

python - 使用 matplotlib 在 Python 中绘制直方图

转载 作者:行者123 更新时间:2023-11-28 16:43:32 24 4
gpt4 key购买 nike

<分区>

我正在为此苦苦挣扎。有些东西我只是没有得到。我有一个函数,我想用 x 轴上的键和 y 轴上的值绘制字典的直方图,然后将文件保存在调用该函数时指定的位置。我拥有的是:

​import matplotlib.pyplot as plt


def test(filename):
dictionary = {0:1000, 1:20, 2:15, 3:0, 4:5}
xmax = max(dictionary.keys())
ymax = max(dictionary.values())
plt.hist(dictionary,xmax)
plt.title('Histogram Title')
plt.xlabel('Label')
plt.ylabel('Another Label')
plt.axis([0, xmax, 0, ymax])
plt.figure()
plt.savefig(filename)

test('test_graph.svg')

我根本无法让它工作,而且我已经努力阅读其他问题和文档很长时间了。任何帮助将不胜感激。谢谢。

编辑:

我的错误是:

File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 343, in figure
**kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 80, in new_figure_manager
window = Tk.Tk()
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1688, in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
TclError: no display name and no $DISPLAY environment variable

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