假设你获取了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
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]
plt.figure(figsize=(20,8),dpi=80)
# 设置组距:之每个小组的两个端点的距离
# max(a)
# min(a)
# max(a)-min(a)
d=3 #设置组距时候,极差的值整除组距需要能够整除掉 在这里极差78 78/3可以整除
#设置组数:=极差/距离
num_bins=(max(a)-min(a))//d #分为多少组
# 绘图
plt.hist(a,num_bins,normed=True)
#传入需要统计的数据,以及组数即可
# normed=True 显示占比是多少 频率
# 设置x轴的刻度
plt.xticks(range(min(a),max(a)+d,d))
#设置网格线
plt.grid(alpha=0.4)
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:WindowsFontsmsyh.ttc")
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] #组距 d
quantity = [836,2737,3723,3926,3596,1438,3273,642,824,613,215,47] #人数
# 确定三组数据的长度是否一样
print(len(interval),len(width),len(quantity))
plt.figure(figsize=(20,8),dpi=80)
plt.bar(range(12),quantity,width=1)
#调整x轴刻度
_x=[i -0.5 for i in range(13)]
_xtick_labels=interval+[150]
plt.xticks(_x,_xtick_labels)
plt.grid()
plt.show()