pyecharts重叠两种图(柱状+折线)

首先官网文档就有
https://pyecharts.org/#/zh-cn/composite_charts
官网的例子是折线图+柱状图×2
http://gallery.pyecharts.org/#/Grid/grid_multi_yaxis
pyecharts重叠两种图(柱状+折线)_第1张图片
当然,如果你的柱状图只有一类的话,稍作修改即可,如下

from pyecharts import options as opts
from pyecharts.charts import Bar, Grid, Line

x_data = ["{}月".format(i) for i in range(1, 13)]
bar = (
    Bar()
    .add_xaxis(x_data)
    .add_yaxis(
        "蒸发量",
        [2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3],
        yaxis_index=0,
        color="#d14a61",
    )
#删除掉额外的柱状分类
#     .add_yaxis(
#         "降水量",
#         [2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3],
#         yaxis_index=1,
#         color="#5793f3",
#     )
#     .extend_axis(
#         yaxis=opts.AxisOpts(
#             name="蒸发量",
#             type_="value",
#             min_=0,
#             max_=250,
#             position="right",
#             axisline_opts=opts.AxisLineOpts(
#                 linestyle_opts=opts.LineStyleOpts(color="#d14a61")
#             ),
#             axislabel_opts=opts.LabelOpts(formatter="{value} ml"),
#         )
#     )
    .extend_axis(
        yaxis=opts.AxisOpts(
            type_="value",
            name="温度",
            min_=0,
            max_=25,
            position="left",
            axisline_opts=opts.AxisLineOpts(
                linestyle_opts=opts.LineStyleOpts(color="#675bba")
            ),
            axislabel_opts=opts.LabelOpts(formatter="{value} °C"),
            splitline_opts=opts.SplitLineOpts(
                is_show=True, linestyle_opts=opts.LineStyleOpts(opacity=1)
            ),
        )
    )
    .set_global_opts(
        yaxis_opts=opts.AxisOpts(
            name="降水量",
            min_=0,
            max_=250,
            position="right",
            offset=0,    #这里是Y轴间距,由80改为0即两个Y轴重合,当然我们已经删除了原来的一个Y轴,所以相当于把第二Y轴左移
            axisline_opts=opts.AxisLineOpts(
                linestyle_opts=opts.LineStyleOpts(color="#5793f3")
            ),
            axislabel_opts=opts.LabelOpts(formatter="{value} ml"),
        ),
        title_opts=opts.TitleOpts(title="Grid-多 Y 轴示例"),
        tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="cross"),
    )
)

line = (
    Line()
    .add_xaxis(x_data)
    .add_yaxis(
        "平均温度",
        [2.0, 2.2, 3.3, 4.5, 6.3, 10.2, 20.3, 23.4, 23.0, 16.5, 12.0, 6.2],
        yaxis_index=1,    #删了一个Y轴,Y轴索引由2改为1
        color="#675bba",
        label_opts=opts.LabelOpts(is_show=False),
    )
)

bar.overlap(line)
grid = Grid()
grid.add(bar, opts.GridOpts(pos_left="5%", pos_right="20%"), is_control_axis_index=True)
grid.render_notebook()

pyecharts重叠两种图(柱状+折线)_第2张图片

你可能感兴趣的:(疑难解决)