使用matplotlib在Python中的直方图
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了使用matplotlib在Python中的直方图,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1900字,纯文字阅读大概需要3分钟。
内容图文
![使用matplotlib在Python中的直方图](/upload/InfoBanner/zyjiaocheng/657/ccba3b7a58e244108a88d1e52e4a7536.jpg)
这个问题已经在这里有了答案: > Generating a PNG with matplotlib when DISPLAY is undefined 12个
我为此感到非常挣扎.有些事情我只是不明白.我有一个函数,我想用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
解决方法:
您会被状态机界面所困扰:
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.figure() # <- makes a new figure and sets it active (add this)
plt.hist(dictionary,xmax) # <- finds the current active axes/figure and plots to it
plt.title('Histogram Title')
plt.xlabel('Label')
plt.ylabel('Another Label')
plt.axis([0, xmax, 0, ymax])
# plt.figure() # <- makes new figure and makes it active (remove this)
plt.savefig(filename) # <- saves the currently active figure (which is empty in your code)
test('test_graph.svg')
有关matplotlib的状态机与OO接口的详细说明,请参见How can I attach a pyplot function to a figure instance?.
内容总结
以上是互联网集市为您收集整理的使用matplotlib在Python中的直方图全部内容,希望文章能够帮你解决使用matplotlib在Python中的直方图所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。