matplotlib(四)直方图和饼状图

频率分布直方图

直方图不同于条形图,直方图更多涉及统计学。横坐标表示每组的端点,纵坐标表示频数,矩形的高表示对应的频数。

简单示例

#有200部电影,统计时长分布状态
a = [114, 130, 127, 91, 103, 140, 138, 141, 144, 147, 131, 145, 138, 150, 133, 123, 145, 145, 125, 128, 129, 135, 153, 98, 110, 139, 136, 150, 141, 131, 125, 138, 105, 114, 110, 106, 115, 101, 110, 93, 99, 98, 91, 114, 118, 109, 98, 105, 100, 113, 106, 91, 120, 104, 114, 123, 115, 100, 128, 104, 103, 104, 92, 120, 92, 114, 102, 92, 119, 115, 102, 96, 91, 99, 116, 105, 111, 100, 110, 113, 108, 120, 119, 116, 110, 118, 119, 102, 118, 103, 118, 102, 126, 114, 132, 130, 118, 106, 109, 126, 122, 101, 122, 108, 110, 140, 108, 112, 103, 113, 123, 130, 127, 123, 123, 131, 120, 109, 133, 126, 119, 119, 104, 95, 105,117, 113, 105, 115, 121, 111, 92, 114, 111, 114, 114, 114, 101, 116, 102, 117, 101, 120, 114, 119, 108, 103, 109, 113, 99, 108, 95, 111, 106, 106, 92, 91, 113, 118, 108, 92, 95, 104, 102, 98, 100, 112, 114, 103, 103, 101, 114, 106, 111, 123, 101, 100, 124, 119, 92, 91, 108, 102, 95, 108, 110, 113, 121, 111, 118, 115, 129, 126, 129, 118, 120, 96, 94, 113, 110]

plt.figure(figsize=(20,8),dpi=80)
d = 2 #组距
num_bins = (max(a) - min(a))//d

# 显示频率density = True
plt.hist(a,num_bins)

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

plt.grid()
plt.show()

简单示例

a.绘制直方图

plt.hist(a,num_bins)

num_bins是组数
density = True 显示频率

b.x轴y轴刻度显示,描述信息,网格

xsticks与yticks:指定坐标轴的刻度
linspace(start, stop, num, endpoint, retstep, dtype)
start:开始值
stop:终值
num:元素个数,默认值50。可选参数
endpoint : 如果是为 True,包括终值stop。默认值为 True。可选参数
retstep : 如果为True,返回 (`samples`, `step`),step表示 samples之间的间距。可选参数
dtype :  输出数组的数据类型.。如果 `dtype` 没有给定,参照其他输入的数据类型。可选参数
#x轴刻度显示
plt.xticks(range(min(a),max(a)+d,d))

#描述信息
plt.xlabel("电影时长")
plt.ylabel("电影数量")
plt.title('电影时长与电影数量分布图')


# 添加网格显示
plt.grid(linestyle=":", alpha=0.5)

在这里插入图片描述

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

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()

在这里插入图片描述


import matplotlib.pyplot as plt
interval = [ 0, 5, 10 , 15 , 20 , 25 , 30 , 35 , 45 , 45 , 60 , 90 ]
width = [ 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 15 , 33 , 60 ]
quantity = [ 836 , 2737 , 3723 , 3926 , 3596 , 1438 , 3273 ,642 ,824, 613, 215, 47 ]

plt.figure(figsize=(20,8),dpi=80)
plt.bar(x = 0,bottom=range(12),width=quantity,height=1,orientation="horizontal")
# p1 = plt.bar(x=0, bottom=y, height=0.5, width=x, orientation="horizontal")
#设置x轴的刻度
_x = [i-0.5 for i in range(13)]
_xtick_labels = interval + [150]

plt.xticks(_x,_xtick_labels)
plt.grid()

plt.show()

在这里插入图片描述

饼状图

简单图例

x = ['天降之物','喜洋洋与灰太狼','鬼灭之刃','一拳超人','魔王大人']
y = [11111,22222,66666,33333,23333]

plt.figure(figsize=(20, 8), dpi=100)
 
# 绘制饼图
plt.pie(y,labels = x, autopct="%1.2f%%", colors=['b','r','g','y','c'])
 
# 显示图例
plt.legend()
 
# 添加标题
plt.title("电影排片占比")

# 添加axis(使图像变圆)
plt.axis('equal')
plt.show()

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值