pyecharts库绘制箱线图
时间: 2023-11-22 10:47:15 浏览: 116
使用pyecharts库绘制箱线图,可以按照以下步骤进行:
1. 导入需要的库和模块:
```python
from pyecharts import options as opts
from pyecharts.charts import Boxplot
```
2. 准备数据,将数据按照一定的格式存储:
```python
data = [
[850, 740, 900, 1070, 930, 850, 950, 980, 980, 880],
[960, 940, 960, 940, 880, 800, 850, 880, 900],
[880, 880, 880, 860, 720, 720, 620, 860],
[890, 810, 810, 820, 800, 770, 760, 740, 750],
[890, 840, 780, 810, 760, 810, 790, 810, 820, 850],
[870, 870, 810, 740, 810, 940, 950, 800, 810],
[840, 850, 850, 780, 890, 810, 820, 800, 840],
[840, 780, 810, 760, 810, 790, 810, 820, 850, 870],
[840, 850, 850, 780, 890, 810, 820, 800, 840],
[840, 780, 810, 760, 810, 790, 810, 820, 850, 870],
[890, 880, 720, 840, 850, 850, 780, 890],
[810, 940, 950, 800, 810, 870, 870, 810, 740, 810],
[840, 850, 850, 780, 890, 810, 820, 800, 840],
[840, 780, 810, 760, 810, 790, 810, 820, 850, 870],
[890, 880, 720, 840, 850, 850, 780, 890],
[810, 940, 950, 800, 810, 870, 870, 810, 740, 810],
[840, 850, 850, 780, 890, 810, 820, 800, 840],
[840, 780, 810, 760, 810, 790, 810, 820, 850, 870],
[890, 880, 720, 840, 850, 850, 780, 890],
[810, 940, 950, 800, 810, 870, 870, 810, 740, 810]
]
```
3. 创建Boxplot对象,设置图表的基本属性:
```python
boxplot = Boxplot()
boxplot.set_global_opts(title_opts=opts.TitleOpts(title="箱线图"))
```
4. 添加数据,设置图表的系列属性:
```python
boxplot.add_xaxis(["expr1", "expr2", "expr3", "expr4", "expr5", "expr6", "expr7", "expr8", "expr9", "expr10", "expr11", "expr12", "expr13", "expr14", "expr15", "expr16", "expr17", "expr18", "expr19", "expr20"])
boxplot.add_yaxis("A", boxplot.prepare_data(data))
```
5. 渲染图表,生成HTML文件:
```python
boxplot.render("boxplot.html")
```
阅读全文