pyecharts折线图
时间: 2023-06-18 09:03:29 浏览: 87
使用 Pyecharts 绘制折线图的步骤如下:
1. 导入需要的模块和数据:
```python
from pyecharts.charts import Line
from pyecharts import options as opts
x_data = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
y_data = [820, 932, 901, 934, 1290, 1330, 1320]
```
2. 创建折线图实例并设置基本配置:
```python
line = Line()
line.set_global_opts(
title_opts=opts.TitleOpts(title="折线图示例"),
tooltip_opts=opts.TooltipOpts(trigger="axis"),
xaxis_opts=opts.AxisOpts(type_="category", boundary_gap=False),
yaxis_opts=opts.AxisOpts(),
)
```
3. 添加数据和设置系列配置:
```python
line.add_xaxis(xaxis_data=x_data)
line.add_yaxis(
series_name="销售额",
y_axis=y_data,
markpoint_opts=opts.MarkPointOpts(
data=[opts.MarkPointItem(type_="max"), opts.MarkPointItem(type_="min")]
),
markline_opts=opts.MarkLineOpts(
data=[opts.MarkLineItem(type_="average")]
),
)
```
4. 渲染图表并保存:
```python
line.render("折线图.html")
```
完整代码如下:
```python
from pyecharts.charts import Line
from pyecharts import options as opts
x_data = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
y_data = [820, 932, 901, 934, 1290, 1330, 1320]
line = Line()
line.set_global_opts(
title_opts=opts.TitleOpts(title="折线图示例"),
tooltip_opts=opts.TooltipOpts(trigger="axis"),
xaxis_opts=opts.AxisOpts(type_="category", boundary_gap=False),
yaxis_opts=opts.AxisOpts(),
)
line.add_xaxis(xaxis_data=x_data)
line.add_yaxis(
series_name="销售额",
y_axis=y_data,
markpoint_opts=opts.MarkPointOpts(
data=[opts.MarkPointItem(type_="max"), opts.MarkPointItem(type_="min")]
),
markline_opts=opts.MarkLineOpts(
data=[opts.MarkLineItem(type_="average")]
),
)
line.render("折线图.html")
```
这样就可以生成一个简单的折线图,保存为 HTML 文件。
阅读全文