Python matplotlib 堆叠图

本文介绍了如何利用Python的matplotlib库创建水平堆叠图和波浪形堆叠图,展示了如何处理数据差异以避免遮挡,并添加数据标签以增强可视化效果。通过调整柱状图宽度和缩放数据,确保所有数据在图表中清晰可见。堆叠图适用于展示不同类别数据在总和中的比例关系。
摘要由CSDN通过智能技术生成

注:本文的所有数据请移步—— 参考数据

一、水平堆叠图

堆叠图其实就是柱状图的一种特殊形式

from matplotlib import pyplot as plt 
plt.style.use('seaborn')
plt.figure(figsize=(15,9))
plt.rcParams.update({'font.family': "Microsoft YaHei"})
plt.title("中国票房2021TOP9") 
plt.bar(cnbodfgbsort.index,cnbodfgbsort.PERSONS)
plt.bar(cnbodfgbsort.index,cnbodfgbsort.PRICE)
plt.bar(cnbodfgbsort.index,cnbodfgbsort.points)
plt.show()

堆叠图效果
在这里插入图片描述
可以看到有部分蓝色的数据被遮挡了,如果我们想全部展现,可以:

index_x=np.arange(len(cnbodfgbsort.index))
index_x
w=0.15
from matplotlib import pyplot as plt 
plt.style.use('classic')
plt.figure(figsize=(15,9))
plt.rcParams.update({'font.family': "Microsoft YaHei"})
plt.title("中国票房2021TOP9")
plt.bar(index_x,cnbodfgbsort.PERSONS,width=w)
plt.bar(index_x+w,cnbodfgbsort.PRICE,width=w)
plt.bar(index_x+2*w,cnbodfgbsort.points,width=w)
plt.show()

在这里插入图片描述

可以看到Excel的数据源当中BO与PRICE和PERSONS的数字相差过大,如果做堆叠图的话,BO会将其他的都进行覆盖,无法显示好的效果:
在这里插入图片描述
因为数据相差实在太大,我们可以直接让BO除以1000

from matplotlib import pyplot as plt 
plt.style.use('classic')
plt.figure(figsize=(15,9))
plt.rcParams.update({'font.family': "Microsoft YaHei"})
plt.title("中国票房2021TOP9") 
plt.bar(cnbodfgbsort.index,cnbodfgbsort.PERSONS)
plt.bar(cnbodfgbsort.index,cnbodfgbsort.PRICE)
plt.bar(cnbodfgbsort.index,cnbodfgbsort.BO/1000)
plt.bar(cnbodfgbsort.index,cnbodfgbsort.points)
plt.show()

在这里插入图片描述

from matplotlib import pyplot as plt 
plt.style.use('classic')
plt.figure(figsize=(15,9))
plt.rcParams.update({'font.family': "Microsoft YaHei"})
plt.title("中国票房2021TOP9")
plt.bar(index_x-w,cnbodfgbsort.BO/1000,width=w)   # 直接让BO除以1000
plt.bar(index_x,cnbodfgbsort.PERSONS,width=w)
plt.bar(index_x+w,cnbodfgbsort.PRICE,width=w)
plt.bar(index_x+2*w,cnbodfgbsort.points,width=w)
plt.show()

在这里插入图片描述

二、波浪形堆叠图

labels=['战争','爱情','动画','动作','惊悚','剧情'] 
colors=['tan','violet','turquoise','tomato','teal','steelblue'] 
plt.stackplot(cnbodfgbsort.index,cnbodfgbsort.PRICE,cnbodfgbsort.PERSONS,cnbodfgbsort.points,labels=labels,colors=colors)

在这里插入图片描述

labels=['战争','爱情','动画','动作','惊悚','剧情'] 
colors=['tan','violet','turquoise','tomato','teal','steelblue'] 
plt.stackplot(cnbodfgbsort.index,cnbodfgbsort.PRICE,cnbodfgbsort.BO/900,cnbodfgbsort.PERSONS,cnbodfgbsort.points,labels=labels,colors=colors)

在这里插入图片描述

三、加上数据标签

plt.legend()
labels=['票房','票价','人次','评分'] 
colors=['tan','violet','turquoise','tomato','teal','steelblue'] 
plt.stackplot(cnbodfgbsort.index,cnbodfgbsort.PRICE,cnbodfgbsort.BO/900,cnbodfgbsort.PERSONS,cnbodfgbsort.points,labels=labels,colors=colors)
plt.legend()

在这里插入图片描述

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

温欣2030

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值