【发布时间】:2011-12-08 19:33:14
【问题描述】:
我想使用 matplotlib 绘制直方图。但是,由于我发送给 hist() 函数的数据量很大(包含大约 100,000 个数字的列表),绘制两个图形时会出现错误。但它进展顺利,同时只绘制了两个情节中的任何一个。谁能帮我处理这个问题?提前致谢。
这是显示错误的简化代码:
f_120 = plt.figure(1)
plt.hist(taccept_list, bins=6000000, normed = True, histtype ="step", cumulative = True, color = 'b', label = 'accepted answer')
plt.hist(tfirst_list, bins=6000000, normed = True, histtype ="step", cumulative = True, color = 'g',label = 'first answer')
plt.axvline(x = 30, ymin = 0, ymax = 1, color = 'r', linestyle = '--', label = '30 min')
plt.axvline(x = 60, ymin = 0, ymax = 1, color = 'c', linestyle = '--', label = '1 hour')
plt.legend()
plt.ylabel('Percentage of answered questions')
plt.xlabel('Minutes elapsed after questions are posted')
plt.title('Cumulative histogram: time elapsed \n before questions receive answer (first 2 hrs)')
plt.ylim(0,1)
plt.xlim(0,120)
f_120.show()
f_120.savefig('7month_0_120.png', format = 'png' )
plt.close()
f_2640 = plt.figure(2)
plt.hist(taccept_list, bins=6000000, normed = True, histtype ="step", cumulative = True, color = 'b', label = 'accepted answer')
plt.hist(tfirst_list, bins=6000000, normed = True, histtype ="step", cumulative = True, color = 'g',label = 'first answer')
plt.axvline(x = 240, ymin = 0, ymax = 1, color = 'r', linestyle = '--', label = '4 hours')
plt.axvline(x = 1440, ymin = 0, ymax = 1, color = 'c', linestyle = '--', label = '1 day')
plt.legend(loc= 4)
plt.ylabel('Percentage of answered questions')
plt.xlabel('Minutes elapsed after questions are posted')
plt.title('Cumulative histogram: time elapsed \n before questions receive answer (first 48)')
plt.ylim(0,1)
plt.xlim(0,2640)
f_2640.show()
f_2640.savefig('7month_0_2640.png', format = 'png' )
以下是错误详情:
plt.hist(tfirst_list, bins=6000000, normed = True, histtype ="step", 累积 = True, color = 'g',label = 'first answer')
文件“C:\software\Python26\lib\site-packages\matplotlib\pyplot.py”,第 2160 行,在 hist ret = ax.hist(x, bins, range, normed, weights,cumulative, bottom, histtype, align,orientation, rwidth, log, color, label, **kwargs)
文件“C:\software\Python26\lib\site-packages\matplotlib\axes.py”,第 7775 行,在 hist 关闭=假,边缘颜色=c,填充=假))
文件“C:\software\Python26\lib\site-packages\matplotlib\axes.py”,第 6384 行,填充 对于 self._get_patches_for_fill(*args, **kwargs) 中的 poly:
文件“C:\software\Python26\lib\site-packages\matplotlib\axes.py”,第 317 行,在 _grab_next_args 对于 self._plot_args(remaining, kwargs) 中的 seg:
文件“C:\software\Python26\lib\site-packages\matplotlib\axes.py”,第 304 行,在 _plot_args seg = func(x[:,j%ncx], y[:,j%ncy], kw, kwargs)
文件“C:\software\Python26\lib\site-packages\matplotlib\axes.py”,第 263 行,在 _makefill (x[:,np.newaxis],y[:,np.newaxis])),
文件“C:\software\Python26\lib\site-packages\numpy\core\shape_base.py”,第 270 行,在 hstack 中 return _nx.concatenate(map(atleast_1d,tup),1)
内存错误
【问题讨论】:
-
请发布回溯。嗯,你为什么要绘制两次相同的数字?
-
你真的需要 600 万个垃圾箱吗?我不明白为什么它无论如何都不应该工作,但是对于这种情况,matplotlib 可能没有非常有效地实现。即使是在 A4 纸上的高质量打印,也只有一万列左右。你的情节分辨率是多少?
-
@rlibby 对不起,我是 matplotlib 的新手,我不知道如何获得我的情节的分辨率。它只是完美地显示了任何一个情节,但在将它们绘制在一起时会崩溃。
-
@Avaris 实际上我想使用相同的数据来显示同一直方图的两个不同范围。并且在运行此脚本时,第一个数字被成功保存,而在处理第二个数字时,显示“内存错误”并且我再次运行以获取回溯
-
@AnneS,如果您不需要查看该图,您可以在不调用
show()的情况下保存它。而且你不需要再次绘图来调整参数xlim,ylim等...只需调整,保存,再次调整,再次保存,...
标签: python matplotlib