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

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]

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

组数要适当,太少会有较大的统计误差,大多规律不明显。
数据分析基础之matplotlib绘制直方图
数据分析基础之matplotlib绘制直方图

代码如下:
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()
显示结果:

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

例题:

      在美国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()
结果显示:

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

那些数据能够绘制直方图

      前面的问题给出的数据都是统计之后的数据,所以为了达到直方图的效果,需要绘制条形图。
      所以,一般来说能够使用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/

上一篇:PXC failed to open gcomm backend connection: 110


下一篇:linux中求两组数据的交集、并集、特有项