pyecharts两个Y轴的折线图怎么画
时间: 2023-09-30 15:07:50 浏览: 240
利用python画出折线图
您可以使用Pyecharts中的Line和Overlap组合两个折线图,并分别设置两个Y轴,如下所示:
```python
from pyecharts.globals import ThemeType
from pyecharts import options as opts
from pyecharts.charts import Line, Overlap
# 构造数据
x_data = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
y1_data = [820, 932, 901, 934, 1290, 1330, 1320]
y2_data = [100, 200, 300, 400, 500, 600, 700]
# 构造第一个折线图,设置第一个Y轴
line1 = (
Line(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))
.add_xaxis(xaxis_data=x_data)
.add_yaxis(
series_name="Line 1",
y_axis=y1_data,
yaxis_index=0,
label_opts=opts.LabelOpts(is_show=False),
)
.set_global_opts(
title_opts=opts.TitleOpts(title="Line 1 and Line 2"),
tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="cross"),
yaxis_opts=opts.AxisOpts(
axislabel_opts=opts.LabelOpts(formatter="{value} ml"),
splitline_opts=opts.SplitLineOpts(is_show=True),
),
)
)
# 构造第二个折线图,设置第二个Y轴
line2 = (
Line(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))
.add_xaxis(xaxis_data=x_data)
.add_yaxis(
series_name="Line 2",
y_axis=y2_data,
yaxis_index=1,
label_opts=opts.LabelOpts(is_show=False),
)
.set_global_opts(
yaxis_opts=opts.AxisOpts(
axislabel_opts=opts.LabelOpts(formatter="{value} ml"),
splitline_opts=opts.SplitLineOpts(is_show=True),
)
)
)
# 将两个折线图叠加在一起
overlap = Overlap()
overlap.add(line1)
overlap.add(line2)
overlap.render("double_y_axis_line_chart.html")
```
运行后会生成一个名为 "double_y_axis_line_chart.html" 的HTML文件,可以在浏览器中打开查看效果。
阅读全文