使用Python画出直方图

https://zhuanlan.zhihu.com/p/476643798

 

它们之间的区别在于pandas.DataFrame.plot.hist将整个dataframe的数据展示在一幅图上,而pandas.DataFrame.hist会调用pandas.DataFrame.plot.hist将dataframe的数据按照不同的列分别展现在不同的图形上。然而,pandas.DataFrame.plot.hist本身调用的是matplotlib.pyplot.hist()。简单点说,pandas里面相关的hist函数都是基于matplotlib里面的pyplot所做的封装。

 

那该如何做?很显然我不可能在df.plot.hist(bins=[...])参数列表里手写20个范围的值,对吧?

一旦明白了自身的需要,那么答案就比较容易了,因为我们看到bins参数需要的是一个列表,虽然不能直接手写20个范围,但我们可以使用Python里面的列表解析方法来便捷的创建列表,也就是我们可以写成bins=[n/100 for n in range(-100, 100, 5)],这样就达到要求了。

 

 

 

ax = df.plot.hist(bins=[n/100 for n in range(-100, 100, 5)])

for bar in ax.patches:
  ax.annotate(format(bar.get_height(), '.0f'),
                   (bar.get_x() + bar.get_width() / 2,
                    bar.get_height()), ha='center', va='center',
                   size=15, xytext=(0, 8),
                   textcoords='offset points')
'''
plot
'''
bins = [n/3 for n in range(-30,31,1)]

intrfr_list_pd = pd.DataFrame(intrfr_list_np)
ax = intrfr_list_pd.plot.hist(bins = [n/3 for n in range(-30,30,1)],grid=True)
ax.set_xlabel('Deg')
ax.set_ylabel('Frame Num')
ax.set_title('DML_L-3_R3')

 

posted @ 2022-09-07 13:38  清风oo  阅读(410)  评论(0)    收藏  举报