python 使用pyecharts构建折柱混合图示例
时间: 2024-06-13 22:08:16 浏览: 24
以下是使用 Pyecharts 构建折柱混合图的示例代码:
```python
from pyecharts import options as opts
from pyecharts.charts import Bar, Line
from pyecharts.faker import Faker
# 生成数据
x = Faker.choose()
y1 = Faker.values()
y2 = Faker.values()
# 构建折柱混合图
bar_line = (
Bar()
.add_xaxis(x)
.add_yaxis("bar", y1)
.set_global_opts(title_opts=opts.TitleOpts(title="折柱混合图示例"))
.extend_axis(
yaxis=opts.AxisOpts(
name="y2",
type_="value",
position="right",
axisline_opts=opts.AxisLineOpts(
linestyle_opts=opts.LineStyleOpts(color="#d14a61")
),
axislabel_opts=opts.LabelOpts(formatter="{value} °C"),
)
)
.add_js_funcs("alert('这是自定义的 JS 代码')")
)
line = (
Line()
.add_xaxis(x)
.add_yaxis("line", y2, yaxis_index=1)
)
bar_line.overlap(line).render("折柱混合图.html")
```
该示例中,我们使用 Pyecharts 的 Bar 和 Line 类分别构建了柱状图和折线图,并使用 overlap() 方法将两个图表叠加在一起。同时,我们还使用了 extend_axis() 方法添加了一个右侧的 Y 轴,并使用 add_js_funcs() 方法添加了自定义的 JavaScript 代码。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)