,可以通过以下步骤实现:
import matplotlib.pyplot as plt
import numpy as np
categories = ['Category 1', 'Category 2', 'Category 3'] # 分类
values1 = [10, 15, 12] # 第一组数据
values2 = [8, 11, 9] # 第二组数据
values3 = [5, 7, 6] # 第三组数据
fig, ax = plt.subplots()
ax.bar(categories, values1, label='Group 1')
ax.bar(categories, values2, bottom=values1, label='Group 2')
ax.bar(categories, values3, bottom=np.array(values1)+np.array(values2), label='Group 3')
ax.set_ylabel('Value')
ax.set_xlabel('Category')
ax.set_title('Stacked Bar Chart')
ax.legend()
plt.show()
average_values = np.mean([values1, values2, values3], axis=0)
fig, ax = plt.subplots()
ax.bar(categories, values1, label='Group 1')
ax.bar(categories, values2, bottom=values1, label='Group 2')
ax.bar(categories, values3, bottom=np.array(values1)+np.array(values2), label='Group 3')
ax.plot(categories, average_values, marker='o', linestyle='-', color='r', label='Average')
ax.set_ylabel('Value')
ax.set_xlabel('Category')
ax.set_title('Stacked Bar Chart with Average Line')
ax.legend()
plt.show()
这样就可以在matplotlib中绘制堆叠条形图的平均折线图了。
关于matplotlib的更多信息和使用方法,可以参考腾讯云的数据可视化产品-云图(Cloud Visualization):云图产品介绍。
没有搜到相关的结果
领取专属 10元无门槛券
手把手带您无忧上云