python绘制多列堆叠柱状图

python绘制多列堆叠柱状图

代码

import matplotlib.pyplot as plt
import numpy as np

tick_label = ["A", "B", "C", "D"]
y = [67, 94, 46, 17]   # left, train
y1 = [48, 67, 37, 13]  # right, train
y2 = [33, 6, 2, 8]     # bilateral, train

y3 = [16, 23, 11, 4]   # left, validation
y4 = [10, 16, 9, 3]  # right, validation
y5 = [10, 2, 1, 2]     # bilateral, validation

y6 = [8, 11, 10, 1]   # left, test
y7 = [11, 10, 6, 7]  # right, test
y8 = [13, 0, 5, 3]     # bilateral, test

x = np.arange(4)
bar_width = 0.3  # 条形宽度
c1 = 'c'
c2 = 'b'
c3 = 'r'
plt.bar(x, y, bar_width, color=c1, label='x1')
plt.bar(x, y1, bar_width, bottom=y,  # 堆叠在第一个上方
        color=c2, label='x2', alpha=0.5)
plt.bar(x, y2, bar_width, bottom=[sum(x) for x in zip(y, y1)],
        color=c3, label='x3', alpha=0.5)

plt.bar(x + bar_width, y3, bar_width,  align="center", color=c1)
plt.bar(x + bar_width, y4, bar_width, bottom=y3,  # 堆叠在第一个上方
        color=c2, alpha=0.5)
plt.bar(x + bar_width, y5, bar_width, bottom=[sum(x) for x in zip(y3, y4)],
        color=c3, alpha=0.5)

plt.bar(x + 2 * bar_width, y6, bar_width,  align="center", color=c1)
plt.bar(x + 2 * bar_width, y7, bar_width, bottom=y6,  # 堆叠在第一个上方
        color=c2, alpha=0.5)
plt.bar(x + 2 * bar_width, y8, bar_width, bottom=[sum(x) for x in zip(y6, y7)],
        color=c3, alpha=0.5)

plt.xticks(x + bar_width / 2, tick_label)
plt.xlabel("Type", fontsize=15)
plt.ylabel("Number", fontsize=15)
plt.legend()
plt.show()

在这里插入图片描述

参考

https://blog.csdn.net/qq_66726657/article/details/131967239

  • 10
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
绘制Python中的多组柱状图,可以使用matplotlib库。在绘制多组柱状图之前,我们需要了解双柱状图和堆积柱状图绘制原理。双柱状图是指在同一张图上绘制两组柱状图,而堆积柱状图是指将两组或更多组柱状图堆叠在一起显示。 对于双柱状图绘制,可以按照以下步骤进行: 1. 导入matplotlib.pyplot库:import matplotlib.pyplot as plt 2. 使用plt.bar()函数绘制两组柱状图,传入相应的数据和参数,例如:plt.bar([1,2,3],[4,5,6]) 3. 使用plt.show()函数显示图像 而对于堆积柱状图绘制,可以按照以下步骤进行: 1. 导入matplotlib.pyplot库:import matplotlib.pyplot as plt 2. 设置中文显示:plt.rcParams["font.sans-serif"] = [u"SimHei"] 3. 设置负号显示:plt.rcParams["axes.unicode_minus"] = False 4. 定义一组柱状体的宽度和一组有几种类别 5. 使用plt.bar()函数绘制每组柱状图,传入相应的数据和参数,例如:plt.bar([x for x in range(seriesNums)], height=[4,5,6], label="男生", width=barWidth) 6. 使用plt.xticks()函数设置x轴刻度,例如:plt.xticks([x barWidth/2*(labelNums-1) for x in range(seriesNums)], ["班级1","班级2","班级3"]) 7. 使用plt.xlabel()和plt.ylabel()函数设置x轴和y轴标题 8. 使用plt.title()函数设置图像标题 9. 使用plt.legend()函数显示图例 10. 使用plt.show()函数显示图像 通过以上步骤,我们可以绘制出多组柱状图。具体的代码示例可以参考引用中的完整代码。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [【数据处理】Python matplotlib绘制柱状图以及绘制堆积柱状图——保姆级教程](https://blog.csdn.net/weixin_39589455/article/details/127938201)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值