使用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 @   清风oo  阅读(409)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· spring官宣接入deepseek,真的太香了~
历史上的今天:
2019-09-07 【面经】MSRA面经 (researcher 8轮面试)
2019-09-07 程序设计与算法(二)算法基础》《第二周 递归》Boolean Expressions(布尔表达式) 2106 HARD
2019-09-07 你的人生也许会经历更多风险
2019-09-07 《程序设计与算法(二)算法基础》《第二周 递归》算24 2787
点击右上角即可分享
微信分享提示