堆积柱形图Python
时间: 2023-11-29 20:46:42 浏览: 67
Python绘制堆叠柱状图的实例
5星 · 资源好评率100%
以下是绘制堆积柱形图的Python代码和解释:
引入库:
```python
import matplotlib.pyplot as plt
import numpy as np
```
数据准备:
```python
# 生成数据
N = 5
men_means = (20, 35, 30, 35, 27)
women_means = (25, 32, 34, 20, 25)
men_std = (2, 3, 4, 1, 2)
women_std = (3, 5, 2, 3, 3)
ind = np.arange(N) # the x locations for the groups
width = 0.35 # the width of the bars: can also be len(x) sequence
```
绘制堆积柱形图:
```python
# 绘制柱形图
p1 = plt.bar(ind, men_means, width, yerr=men_std)
p2 = plt.bar(ind, women_means, width,
bottom=men_means, yerr=women_std)
# 添加图例、标签和标题
plt.ylabel('Scores')
plt.title('Scores by group and gender')
plt.xticks(ind, ('G1', 'G2', 'G3', 'G4', 'G5'))
plt.yticks(np.arange(0, 81, 10))
plt.legend((p1[0], p2[0]), ('Men', 'Women'))
# 显示图形
plt.show()
```
相关问题:
阅读全文