pyecharts绘制双y轴图生成html文件
时间: 2023-12-26 09:07:14 浏览: 73
数据可视化-Pyecharts绘制折线图
要使用Pyecharts绘制双Y轴图并生成HTML文件,可以使用Line类和Overlap类。下面是一个简单的例子:
```python
from pyecharts import options as opts
from pyecharts.charts import Line, Bar, Grid, Overlap
from pyecharts.commons.utils import JsCode
# 构造数据
x_data = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
y1_data = [820, 932, 901, 934, 1290, 1330, 1320]
y2_data = [200, 182, 191, 234, 290, 330, 310]
# 创建第一条线
line = (
Line()
.add_xaxis(xaxis_data=x_data)
.add_yaxis(
series_name="Line 1",
y_axis=y1_data,
label_opts=opts.LabelOpts(
is_show=False
),
linestyle_opts=opts.LineStyleOpts(
width=2,
type_='dashed'
),
itemstyle_opts=opts.ItemStyleOpts(
color='blue'
)
)
)
# 创建第二条线
bar = (
Bar()
.add_xaxis(xaxis_data=x_data)
.add_yaxis(
series_name="Bar 1",
y_axis=y2_data,
label_opts=opts.LabelOpts(
is_show=False
),
itemstyle_opts=opts.ItemStyleOpts(
color='red'
)
)
)
# 将两个图形叠加在一起
overlap = Overlap()
overlap.add(line)
overlap.add(bar, yaxis_index=1)
# 设置双Y轴
overlap.set_global_opts(
yaxis_opts=[
opts.AxisOpts(
type_="value",
name="Line Y",
axislabel_opts=opts.LabelOpts(formatter="{value}"),
),
opts.AxisOpts(
type_="value",
name="Bar Y",
position="right",
axislabel_opts=opts.LabelOpts(formatter="{value}"),
),
]
)
# 将图形保存为HTML文件
overlap.render("chart.html")
```
在这个例子中,我们首先创建了两个数据系列,然后使用Line和Bar类分别创建两个图形。最后,我们使用Overlap类将这两个图形叠加在一起,并使用set_global_opts方法设置双Y轴。最后,我们将图形保存为HTML格式的文件。
阅读全文