pyecharts绘制饼图
时间: 2023-09-20 20:07:29 浏览: 149
使用 pyecharts 绘制饼图的步骤如下:
1. 安装 pyecharts
```
pip install pyecharts
```
2. 导入必要的库和模块
```python
from pyecharts import options as opts
from pyecharts.charts import Pie
```
3. 准备数据
```python
data = [
("Apple", 55),
("Banana", 20),
("Pear", 15),
("Grape", 30),
("Orange", 40),
]
```
4. 绘制饼图
```python
pie = (
Pie()
.add("", data)
.set_colors(["blue", "green", "yellow", "purple", "orange"])
.set_global_opts(
title_opts=opts.TitleOpts(title="Fruit Distribution"),
legend_opts=opts.LegendOpts(orient="vertical", pos_top="15%", pos_left="2%"),
)
.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
)
pie.render("pie.html")
```
5. 运行脚本并生成饼图
```python
python script.py
```
这样就可以在当前目录下生成一个名为 `pie.html` 的文件,用浏览器打开即可看到饼图。
阅读全文