python数据分析--Matplotlib绘制分组条形图和堆叠条形图

博客主要介绍了使用Matplotlib进行绘图,重点阐述了分组条形图和堆叠条形图的绘制。借助Python工具,实现数据的可视化展示,为数据分析提供直观的呈现方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Matplotlib绘制–>分组条形图、堆叠条形图

一、分组条形图

# @Author  : Keep
# @File    : 分组条形图.py

from matplotlib import pyplot as plt
from matplotlib.font_manager import FontProperties

# 导入字体
font = FontProperties(fname=r"C:\Windows\Fonts\simsun.ttc",size=12)


fruits = ["苹果","梨子","车厘子","香蕉"]
q1_sales = [1000,800,3000,5000]
q2_sales = [1200,700,2800,4500]

# 绘图(在一个刻度的两边分别绘制两条柱状图)
width = 0.3 # 设置一个固定宽度
po_l = [i-width/2 for i in range(len(fruits))]
po_r = [i+width/2 for i in range(len(fruits))]
# print(po_l,po_r)
# [-0.15, 0.85, 1.85, 2.85] [0.15, 1.15, 2.15, 3.15]

plt.bar(po_l,q1_sales,width=width,label='Q1_sales')
plt.bar(po_r,q2_sales,width=width,label='Qw_sales')

# 设置刻度
plt.xticks(range(len(fruits)),fruits,fontproperties=font)

# 设置坐标标签
plt.xlabel('品种',fontproperties=font)
plt.ylabel('销售额',fontproperties=font)

# 设置图例
plt.legend()

# 展示
plt.show()

二、堆叠条形图

# @Author  : Keep
# @File    : 堆叠条型图.py

from matplotlib import pyplot as plt
from matplotlib.font_manager import FontProperties

font = FontProperties(fname=r"C:\Windows\Fonts\simsun.ttc",size=12)

fruits = ["苹果","梨子","车厘子","香蕉"]
q1_sales = [1000,800,3000,5000]
q2_sales = [1200,700,2800,4500]

# 绘图
plt.bar(fruits,q1_sales,width=0.3,label='Q1_sales',)
plt.bar(fruits,q2_sales,width=0.3,label='Qw_sales',)

# 设置刻度
plt.xticks(range(len(fruits)),fruits,fontproperties=font)

# 设置坐标标签
plt.xlabel('品种',fontproperties=font)
plt.ylabel('销售额',fontproperties=font)

# 设置图例
plt.legend()

# 展示
plt.show()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

pinuscembra

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

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

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

打赏作者

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

抵扣说明:

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

余额充值