要制作堆叠柱形图,可以使用Python中的matplotlib库。以下是一个示例代码,展示如何制作堆叠柱形图:

import matplotlib.pyplot as plt

# 准备数据
labels = ['A', 'B', 'C']
data1 = [10, 20, 30]
data2 = [5, 15, 25]
data3 = [15, 10, 5]

# 绘制图形
fig, ax = plt.subplots()
ax.bar(labels, data1, label='Data 1')
ax.bar(labels, data2, bottom=data1, label='Data 2')
ax.bar(labels, data3, bottom=[i+j for i,j in zip(data1,data2)], label='Data 3')

# 添加图例和标签
ax.set_ylabel('Value')
ax.set_xlabel('Category')
ax.legend()

# 显示图形
plt.show()

在这个示例中,我们准备了三组柱形图的数据,分别用data1data2data3表示。然后,我们使用ax.bar()函数绘制柱形图,通过bottom参数指定每组数据在堆叠柱形图中的位置。最后,我们添加了图例和标签,并使用plt.show()函数显示图形

python如何制作堆叠柱形图

原文地址: https://www.cveoy.top/t/topic/fslR 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录