matplotlib 并列堆叠图
时间: 2023-11-13 14:54:52 浏览: 78
要绘制 matplotlib 并列堆叠图,可以使用 `bar` 函数,并设置 `bottom` 参数来实现。具体步骤如下:
1. 创建一个 `numpy` 数组,用于存储每个类别的数值。
2. 使用 `bar` 函数绘制第一个类别的柱状图,并将 `bottom` 参数设置为 0。
3. 使用 `bar` 函数绘制第二个类别的柱状图,并将 `bottom` 参数设置为第一个类别的数值。
4. 重复步骤 3,绘制其他类别的柱状图。
以下是一个示例代码:
```python
import numpy as np
import matplotlib.pyplot as plt
# 创建数据
data1 = np.array([1, 2, 3, 4])
data2 = np.array([2, 3, 1, 5])
data3 = np.array([3, 1, 4, 2])
# 绘制并列堆叠图
plt.bar(np.arange(len(data1)), data1, label='data1')
plt.bar(np.arange(len(data2)), data2, bottom=data1, label='data2')
plt.bar(np.arange(len(data3)), data3, bottom=data1+data2, label='data3')
# 添加图例和标签
plt.legend()
plt.xlabel('x')
plt.ylabel('y')
plt.title('Stacked Bar Chart')
# 显示图形
plt.show()
```
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pptx](https://img-home.csdnimg.cn/images/20241231044947.png)