图表matplotlib在直方图箱中显示总计数 [英] Graph matplotlib to show total count in the histogram bins
问题描述
我正在尝试显示直方图.下面的数组是直方图的每个单独 bin 的计数.
binVals = [0,5531608,6475325,1311915,223000,609638,291151,449434,1398731,2516755,3035532,2976924,2695079,1822865,1347155,304911,3562,157,5,0,0,0,0,0,0,0,0,0]
我该怎么做?我尝试使用此代码
将matplotlib.pyplot导入为pltbinVals = [0,5531608,6475325,1311915,223000,609638,291151,449434,1398731,2516755,3035532,2976924,2695079,1822865,1347155,304911,3562,157,5,0,0,0,0,0,0,0,0,0]plt.hist(binVals, bins=len(binVals), color='r', alpha=0.5, label='Values')plt.title("演示直方图")plt.xlabel("值")plt.ylabel("频率")plt.legend()plt.show()
这将返回与此类似的图像
不过这是关闭的,因为 bin 0 和 19 到 27 在 binVals
中的计数为零.上图中的零值不在我预期的位置.
我期待类似的东西
如何修改我的代码以获得这种类型的结果?
作为一个奖励问题,在预期图形的底部,有一个标为Bins的标签.可以使用matlibplot完成吗?
我认为你在策划错误的事情.Matplotlib将计算直方图本身.现在,它绘制出具有14个bin的值在0到250,000之间,有3个bin的值在250,000到500,000之间,等等.如果您自己计算直方图,请使用bar()图,或者让Matplotlib为您计算直方图./p>
对于刻度标签,使用 set_xticklabels(["Under 600", "600-700",...], rotation = "vertical")
(我把评论变成了答案,所以可以标记为已回答)
I am attempting to show a histogram. The array below is the count for each individual bin of the histogram.
binVals = [0,5531608,6475325,1311915,223000,609638,291151,449434,1398731,2516755,3035532,2976924,2695079,1822865,1347155,304911,3562,157,5,0,0,0,0,0,0,0,0,0]
How would I go about doing this? I attempted with this code
import matplotlib.pyplot as plt
binVals = [0,5531608,6475325,1311915,223000,609638,291151,449434,1398731,2516755,3035532,2976924,2695079,1822865,1347155,304911,3562,157,5,0,0,0,0,0,0,0,0,0]
plt.hist(binVals, bins=len(binVals), color='r', alpha=0.5, label='Values')
plt.title("Demo Histogram")
plt.xlabel("Value")
plt.ylabel("Frequency")
plt.legend()
plt.show()
This returns an image similar to this
This is off though, because bins 0 and 19 through 27 have a count of zero in binVals
. The zero values in the image above are not in the locations I'd expect.
I am expecting something similar to this
How can I modify my code to get this type of result?
As a bonus question, at the bottom of the expected graph there are nicely labeled Bins. Can this be done with matlibplot?
I think you're plotting the wrong thing. Matplotlib will calculate the histogram itself. It now plots that you have 14 bins with a value between 0 and 250,000, 3 with a value between 250,000 and 500,000 etc. If you calculate the histogram yourself, use a bar() plot, or let Matplotlib calculate the histogram for you.
For the tick labels, use set_xticklabels(["Under 600", "600-700",...], rotation = "vertical")
(I made the comments an answer, so it can be marked as answered)
这篇关于图表matplotlib在直方图箱中显示总计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!