如何用pycharts绘制堆叠条形图
时间: 2023-12-23 11:06:07 浏览: 90
Python 堆叠柱状图绘制方法
PyCharts是一个非常简单易用的Python图表库,可以用它来绘制各种类型的图表。如果要绘制堆叠条形图,可以按照以下步骤进行:
1. 安装PyCharts库:
```python
pip install pyecharts
```
2. 导入Bar模块:
```python
from pyecharts.charts import Bar
```
3. 创建一个Bar对象,并设置标题和x轴数据:
```python
bar = Bar()
bar.set_global_opts(title_opts={"text": "堆叠条形图"})
bar.add_xaxis(["A", "B", "C", "D"])
```
4. 添加y轴数据,并设置堆叠方式:
```python
bar.add_yaxis("类别1", [10, 20, 30, 40], stack="stack1")
bar.add_yaxis("类别2", [5, 15, 25, 35], stack="stack1")
bar.add_yaxis("类别3", [15, 25, 35, 45], stack="stack1")
```
5. 显示图表:
```python
bar.render("堆叠条形图.html")
```
完整代码示例:
```python
from pyecharts.charts import Bar
bar = Bar()
bar.set_global_opts(title_opts={"text": "堆叠条形图"})
bar.add_xaxis(["A", "B", "C", "D"])
bar.add_yaxis("类别1", [10, 20, 30, 40], stack="stack1")
bar.add_yaxis("类别2", [5, 15, 25, 35], stack="stack1")
bar.add_yaxis("类别3", [15, 25, 35, 45], stack="stack1")
bar.render("堆叠条形图.html")
```
阅读全文