以下是一个使用matplotlib库绘制不同histtype的堆叠柱状图的示例代码:
import matplotlib.pyplot as plt
# 数据
labels = ['A', 'B', 'C']
data1 = [10, 15, 7]
data2 = [12, 8, 10]
data3 = [5, 9, 12]
# 创建图形和坐标轴对象
fig, ax = plt.subplots()
# 绘制堆叠柱状图
width = 0.35 # 柱状图的宽度
x = range(len(data1)) # x轴的位置
# 绘制第一个数据集的柱状图
rects1 = ax.bar(x, data1, width, label='Data 1', alpha=0.7, color='blue', edgecolor='black', linewidth=1, hatch="/")
# 绘制第二个数据集的柱状图
rects2 = ax.bar(x, data2, width, bottom=data1, label='Data 2', alpha=0.7, color='green', edgecolor='black', linewidth=1, hatch="o")
# 绘制第三个数据集的柱状图
rects3 = ax.bar(x, data3, width, bottom=[i+j for i,j in zip(data1, data2)], label='Data 3', alpha=0.7, color='red', edgecolor='black', linewidth=1, hatch="\\")
# 设置图例
ax.legend()
# 设置x轴和y轴标签
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
# 设置x轴刻度标签
ax.set_xticks(x)
ax.set_xticklabels(labels)
# 显示图形
plt.show()
在上述代码中,我们使用了matplotlib的bar
函数来绘制堆叠柱状图。其中,bottom
参数用于指定上一层柱状图的高度,从而实现堆叠效果。histtype
的不同可以通过调整alpha
、color
、edgecolor
、linewidth
和hatch
等参数来实现。
上一篇:不同合约账户的以太坊并发性
下一篇:不同后端并发的过程调用。