pyecharts标记线、多图叠加、双坐标轴示例

pyecharts标记线、多图叠加、双坐标轴示例

  • pyecharts官方文档及示例
    • 标记线
    • 多图叠加
    • 多坐标轴

pyecharts官方文档及示例

官方文档:http://pyecharts.org/#/zh-cn/intro
示例:https://gallery.pyecharts.org/#/README
适用笵围:V1.7以上

标记线

标记线属于系列配置项,故应添加在.set_series_opts()中。
代码示例:

from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.faker import Faker
c = (
    Bar()
    .add_xaxis(Faker.choose())
    .add_yaxis("商家A", Faker.values())
    .set_global_opts(title_opts=opts.TitleOpts(title="Bar-MarkLine(指定类型)"))
    .set_series_opts(
        label_opts=opts.LabelOpts(is_show=False),
        markline_opts=opts.MarkLineOpts(
            data=[
                opts.MarkLineItem(type_="min", name="最小值"),      #插入最小值标记线
                opts.MarkLineItem(type_="max", name="最大值"),      #插入最大值标记线
                opts.MarkLineItem(type_="average", name="平均值"),      #插入最大值标记线
				opts.MarkLineItem(y=0, name="yAxis=0")             #插入y轴=0的标记线
            ]
        )
c.render('图表.html')

效果:
pyecharts标记线、多图叠加、双坐标轴示例_第1张图片

多图叠加

多图叠加使用的是overlap组件。
如需做柱状图及折线图叠加,可分别做bar及line的设置,之后用bar.overlap(line)命令叠加。
代码示例:
http://gallery.pyecharts.org/#/Overlap/overlap_bar_line

from pyecharts import options as opts
from pyecharts.charts import Bar, Line
from pyecharts.faker import Faker

v1 = [2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3]
v2 = [2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3]
v3 = [2.0, 2.2, 3.3, 4.5, 6.3, 10.2, 20.3, 23.4, 23.0, 16.5, 12.0, 6.2]


bar = (
    Bar()
    .add_xaxis(Faker.months)
    .add_yaxis("蒸发量", v1)
    .add_yaxis("降水量", v2)
    .extend_axis(
        yaxis=opts.AxisOpts(
            axislabel_opts=opts.LabelOpts(formatter="{value} °C"), interval=5
        )
    )
    .set_series_opts(label_opts=opts.LabelOpts(is_show=False))
    .set_global_opts(
        title_opts=opts.TitleOpts(title="Overlap-bar+line"),
        yaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(formatter="{value} ml")),
    )
)

line = Line().add_xaxis(Faker.months).add_yaxis("平均温度", v3, yaxis_index=1)
bar.overlap(line)     #在柱状图上叠加折线图
bar.render("overlap_bar_line.html")

效果:
pyecharts标记线、多图叠加、双坐标轴示例_第2张图片

多坐标轴

示例代码:
https://gallery.pyecharts.org/#/Bar/multiple_y_axes

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

colors = ["#5793f3", "#d14a61", "#675bba"]
x_data = ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"]
legend_list = ["蒸发量", "降水量", "平均温度"]
evaporation_capacity = [    2.0,    4.9,    7.0,    23.2,    25.6,    76.7,    135.6,    162.2,    32.6,    20.0,    6.4,    3.3,]
rainfall_capacity = [    2.6,    5.9,    9.0,    26.4,    28.7,    70.7,    175.6,    182.2,    48.7,    18.8,    6.0,    2.3,]
average_temperature = [2.0, 2.2, 3.3, 4.5, 6.3, 10.2, 20.3, 23.4, 23.0, 16.5, 12.0, 6.2]

bar = (
    Bar(init_opts=opts.InitOpts(width="1680px", height="800px"))
    .add_xaxis(xaxis_data=x_data)
    .add_yaxis(            
        series_name="蒸发量",
        yaxis_data=evaporation_capacity,
        yaxis_index=0,
        color=colors[1],
    )
    #额外添加两个y轴
    .add_yaxis(
        series_name="降水量", yaxis_data=rainfall_capacity, yaxis_index=1, color=colors[0]
    )
    .extend_axis(
        yaxis=opts.AxisOpts(
            name="蒸发量",
            type_="value",
            min_=0,
            max_=250,
            position="right",
            axisline_opts=opts.AxisLineOpts(
                linestyle_opts=opts.LineStyleOpts(color=colors[1])
            ),
            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=colors[2])
            ),
            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(
            type_="value",
            name="降水量",
            min_=0,
            max_=250,
            position="right",
            offset=80,
            axisline_opts=opts.AxisLineOpts(
                linestyle_opts=opts.LineStyleOpts(color=colors[0])
            ),
            axislabel_opts=opts.LabelOpts(formatter="{value} ml"),
        ),
        tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="cross"),
    )
)

line = (
    Line()
    .add_xaxis(xaxis_data=x_data)
    .add_yaxis(
        series_name="平均温度", y_axis=average_temperature, yaxis_index=2, color=colors[2]
    )
)

bar.overlap(line).render("multiple_y_axes.html")

效果:
pyecharts标记线、多图叠加、双坐标轴示例_第3张图片

你可能感兴趣的:(python)