ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

数据分析基础之matplotlib绘制直方图

2019-08-10 17:42:49  阅读:312  来源: 互联网

标签:数据分析 plt 117 matplotlib 直方图 123 111 110 112


matplotlib绘制直方图

      假设你获取了250部电影的时长(列表a中),希望统计出这些电影时长的分布状态(比如时长为100分钟到120分钟电影的数量,出现的频率)等信息,你应该如何呈现这些数据?

a=[131, 98, 125, 131, 124, 139, 131, 117, 128, 108, 135, 138, 131, 102, 107, 114, 119, 128, 121, 142, 127, 130, 124, 101, 110, 116, 117, 110, 128, 128, 115, 99, 136, 126, 134, 95, 138, 117, 111,78, 132, 124, 113, 150, 110, 117, 86, 95, 144, 105, 126, 130,126, 130, 126, 116, 123, 106, 112, 138, 123, 86, 101, 99, 136,123, 117, 119, 105, 137, 123, 128, 125, 104, 109, 134, 125, 127,105, 120, 107, 129, 116, 108, 132, 103, 136, 118, 102, 120, 114,105, 115, 132, 145, 119, 121, 112, 139, 125, 138, 109, 132, 134,156, 106, 117, 127, 144, 139, 139, 119, 140, 83, 110, 102,123,107, 143, 115, 136, 118, 139, 123, 112, 118, 125, 109, 119, 133,112, 114, 122, 109, 106, 123, 116, 131, 127, 115, 118, 112, 135,115, 146, 137, 116, 103, 144, 83, 123, 111, 110, 111, 100, 154,136, 100, 118, 119, 133, 134, 106, 129, 126, 110, 111, 109, 141,120, 117, 106, 149, 122, 122, 110, 118, 127, 121, 114, 125, 126,114, 140, 103, 130, 141, 117, 106, 114, 121, 114, 133, 137, 92,121, 112, 146, 97, 137, 105, 98, 117, 112, 81, 97, 139, 113,134, 106, 144, 110, 137, 137, 111, 104, 117, 100, 111, 101, 110,105, 129, 137, 112, 120, 113, 133, 112, 83, 94, 146, 133, 101,131, 116, 111, 84, 137, 115, 122, 106, 144, 109, 123, 116, 111,111, 133, 150]

把数据分成多少组进行统计?

组数要适当,太少会有较大的统计误差,大多规律不明显。
在这里插入图片描述
在这里插入图片描述

代码如下:
from matplotlib import pyplot as plt
from matplotlib import font_manager

my_font = font_manager.FontProperties(fname="C:/Windows/Fonts/SIMYOU.TTF")

plt.figure(figsize=(20,8),dpi=80)

a=[131,98,125,131,124,139,131,117,128,108,135,138,131,102,107,114,119, 128, 121, 142, 127, 130, 124, 101, 110, 116, 117, 110, 128, 128, 115,  99, 136, 126, 134,  95, 138, 117, 111,78, 132, 124, 113, 150, 110, 117,  86,  95, 144, 105, 126, 130,126, 130, 126, 116, 123, 106, 112, 138, 123,  86, 101,  99, 136,123, 117, 119, 105, 137, 123, 128, 125, 104, 109, 134, 125, 127,105, 120, 107, 129, 116, 108, 132, 103, 136, 118, 102, 120, 114,105, 115, 132, 145, 119, 121, 112, 139, 125, 138, 109, 132, 134,156, 106, 117, 127, 144, 139, 139, 119, 140,  83, 110, 102,123,107, 143, 115, 136, 118, 139, 123, 112, 118, 125, 109, 119, 133,112, 114, 122, 109, 106, 123, 116, 131, 127, 115, 118, 112, 135,115, 146, 137, 116, 103, 144,  83, 123, 111, 110, 111, 100, 154,136, 100, 118, 119, 133, 134, 106, 129, 126, 110, 111, 109, 141,120, 117, 106, 149, 122, 122, 110, 118, 127, 121, 114, 125, 126,114, 140, 103, 130, 141, 117, 106, 114, 121, 114, 133, 137,  92,121, 112, 146,  97, 137, 105,  98, 117, 112,  81,  97, 139, 113,134, 106, 144, 110, 137, 137, 111, 104, 117, 100, 111, 101, 110,105, 129, 137, 112, 120, 113, 133,112,83,94,146,133,101,131,116,111,84,137,115,122,106,144,109,123,116,111,111,133,150]

# 计算组数
# d:组距
d = 3
num_bins = (max(a)-min(a))//d
print(min(a),max(a),max(a)-min(a))

plt.hist(a,num_bins,normed=True)

# 设置x轴的刻度
plt.xticks(range(min(a),max(a)+d,d))

# 绘制网格
plt.grid(alpha=0.4)

plt.xlabel("电影时长",fontproperties=my_font)
plt.ylabel("数量",fontproperties=my_font)
plt.title("标题",fontproperties=my_font)

plt.show()
显示结果:

在这里插入图片描述

例题:

      在美国2004年人口普查发现有124 million的人在离家相对较远的地方工作。根据他们从家到上班地点所需要的时间,通过抽样统计(最后一列)出了下表的数据,这些数据能够绘制成直方图么?
interval = [0,5,10,15,20,25,30,35,40,45,60,90]
width = [5,5,5,5,5,5,5,5,5,15,30,60]
quantity = [836,2737,3723,3926,3596,1438,3273,642,824,613,215,47]

代码如下:
from matplotlib import pyplot as plt
from matplotlib import font_manager

my_font = font_manager.FontProperties(fname="C:/Windows/Fonts/SIMYOU.TTF")

x_interval = [0,5,10,15,20,25,30,35,40,45,60,90]
width = [5,5,5,5,5,5,5,5,5,15,30,60]
y_quantity = [836,2737,3723,3926,3596,1438,3273,642,824,613,215,47]

plt.figure(figsize=(20,8),dpi=80)

plt.bar(range(len(y_quantity)),y_quantity,width=1)

# 设置x轴的刻度
_x = [i-0.5 for i in range(12)]
_xticks_labels = x_interval + [150]
plt.xticks(_x,_xticks_labels)

plt.grid(alpha=0.4)
plt.xlabel("从家到上班地点所需要的时间",fontproperties=my_font)
plt.ylabel("人数",fontproperties=my_font)
plt.title("标题",fontproperties=my_font)
plt.show()
结果显示:

在这里插入图片描述

那些数据能够绘制直方图

      前面的问题给出的数据都是统计之后的数据,所以为了达到直方图的效果,需要绘制条形图。
      所以,一般来说能够使用plt.hist方法的的hi那些没有统计过的数据。

直方图的应用场景

  1. 用户的年龄分布状态
  2. 一段时间内用户点击次数的分布状态
  3. 用户活跃的分布状态

matplotlib常见问题总结

  1. 应该选择那种图形来呈现数据
  2. 折线图:matplotlib.plot(x,y)
  3. 条形图:matplotlib.bar(x,y)
  4. 柱状图:matplotlib.hist(data,bins,normed)
  5. 散点图:matplotlib.scatter(x,y)
  6. xticks和yticks的设置
  7. label和title,grid的设置
  8. 绘图大小和保存图片

matplotlib使用流程总结

  1. 明确问题
  2. 选择图形的呈现形式
  3. 准备数据
  4. 绘图和图形完善

matplotlib更多的图形样式

      matplotlib支持的图形非常多,如果有其他需求,可以查看下面url地址:http://matplotlib.org/gallery/index.html

plotly

      可视化工具的github,相比于matplotlib更加简单,图形更加漂亮,同时兼容matplotlib和pandas。
使用用法:简单,照着文档即可
文档地址:https://plot.ly/python/

标签:数据分析,plt,117,matplotlib,直方图,123,111,110,112
来源: https://blog.csdn.net/weixin_44953902/article/details/98997888

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有