欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

PyEcharts 直角坐标系图表之折线/面积图

程序员文章站 2022-05-26 22:02:45
...

目录

第1关:Line:折线图(一)

第2关:Line:折线图(二)

第3关:Line:折线图(三)

第4关:Line:折线图(四)

第5关:Line:折线图(五)

第6关:Line:折线图(六)

第7关:Line:折线图(七)

第8关:Line:折线图(八)


第1关:Line:折线图(一)

from PreTest import *
from pyecharts import options as opts
from pyecharts.render import make_snapshot
from snapshot_phantomjs import snapshot

from pyecharts.charts import Line


x_data = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
y_data = [820, 932, 901, 934, 1290, 1330, 1320]


def line_chart() -> Line:
    # ********* Begin *********#  
    line = (
        Line()
        .add_xaxis(x_data)
        .add_yaxis("",y_data,symbol='emptyCircle',is_symbol_show=True,label_opts=opts.LabelOpts(is_show=False))
        .set_global_opts(
            tooltip_opts=opts.TooltipOpts(is_show=False),
            xaxis_opts=opts.AxisOpts(
                type_="category"
            ),
            yaxis_opts=opts.AxisOpts(
                type_="value",
                axistick_opts=opts.AxisTickOpts(is_show=True),
                splitline_opts=opts.SplitLineOpts(is_show=True)
            ),
        )
    )
    # ********** End **********#
    return line

make_snapshot(snapshot, line_chart().render("Result/render.html"), 'StudentAnswer/student_answer.png') # 输出图片
make_snapshot(snapshot, line_base(x_data, y_data).render(), "StandardAnswer/task1/standard_answer_1.png")

第2关:Line:折线图(二)

from PreTest import *
from pyecharts import options as opts
from pyecharts.faker import Faker
from pyecharts.render import make_snapshot
from snapshot_phantomjs import snapshot

from pyecharts.charts import Line

data_x = Faker.choose()
data_y_1 = Faker.values()
data_y_2 = Faker.values()


def line_chart() -> Line:
    # ********* Begin *********#  
    line = (
        Line()
        .add_xaxis(data_x)
        .add_yaxis("商家A",data_y_1)
        .add_yaxis("商家B",data_y_2)
        .set_global_opts(
            title_opts=opts.TitleOpts(title="Line-面积图")
            
        )
        .set_series_opts(
            areastyle_opts=opts.AreaStyleOpts(opacity=0.5)
        )
    )
    # ********** End **********#
    return line

make_snapshot(snapshot, line_chart().render("Result/render.html"), "StudentAnswer/student_answer.png") # 输出图片
make_snapshot(snapshot, line_area_style(data_x, data_y_1, data_y_2).render(), "StandardAnswer/task2/standard_answer_2.png")

第3关:Line:折线图(三)

from PreTest import *
from pyecharts import options as opts
from pyecharts.render import make_snapshot
from snapshot_phantomjs import snapshot

from pyecharts.charts import Line

from pyecharts.faker import Faker

data_x = Faker.choose()
data_y = Faker.values()


def line_chart() -> Line:
    # ********* Begin *********#  
    line = (
        Line()
        .add_xaxis(data_x)
        .add_yaxis(
            "商家A",
            data_y,
            symbol_size=20,
            symbol='triangle',
            linestyle_opts=opts.LineStyleOpts(width=4,type_='dashed',color='green'),
            itemstyle_opts=opts.ItemStyleOpts(border_width=3,border_color="yellow",color='blue')
            )
        .set_global_opts(
            title_opts=opts.TitleOpts(title="Line-ItemStyle")
        )

    )
    # ********** End **********#
    return line

make_snapshot(snapshot, line_chart().render("Result/render.html"), 'StudentAnswer/student_answer.png') # 输出图片
make_snapshot(snapshot, line_itemstyle(data_x, data_y).render(), "StandardAnswer/task3/standard_answer_3.png")

第4关:Line:折线图(四)

from PreTest import *
from pyecharts import options as opts
from pyecharts.render import make_snapshot
from snapshot_phantomjs import snapshot

from pyecharts.charts import Line

x_data = ["一", "二", "三", "四", "五", "六", "七", "八", "九"]
y_data_3 = [1, 3, 9, 27, 81, 247, 741, 2223, 6669]
y_data_2 = [1, 2, 4, 8, 16, 32, 64, 128, 256]
y_data_05 = [1 / 2, 1 / 4, 1 / 8, 1 / 16, 1 / 32, 1 / 64, 1 / 128, 1 / 256, 1 / 512]


def line_chart() -> Line:
    # ********* Begin *********#  
    line = (
        Line(init_opts=opts.InitOpts(width="1600px", height="800px"))
        .add_xaxis(xaxis_data=x_data)
        .add_yaxis(
            series_name="1/2的指数",
            y_axis=y_data_05,
            linestyle_opts=opts.LineStyleOpts(width=2),
        )
        .add_yaxis(
            series_name="2的指数", y_axis=y_data_2, linestyle_opts=opts.LineStyleOpts(width=2)
        )
        .add_yaxis(
            series_name="3的指数", y_axis=y_data_3, linestyle_opts=opts.LineStyleOpts(width=2)
        )
        .set_global_opts(
            title_opts=opts.TitleOpts(title="对数轴示例", pos_left="center"),
            tooltip_opts=opts.TooltipOpts(trigger="item", formatter="{a} <br/>{b} : {c}"),
            legend_opts=opts.LegendOpts(pos_left="left"),
            xaxis_opts=opts.AxisOpts(type_="category", name="x"),
            yaxis_opts=opts.AxisOpts(
                type_="log",
                name="y",
                splitline_opts=opts.SplitLineOpts(is_show=True),
                is_scale=True,
            ),
        )
    )
    # ********** End **********#
    return line

make_snapshot(snapshot, line_chart().render("Result/render.html"), 'StudentAnswer/student_answer.png') # 输出图片
make_snapshot(snapshot, line_log_axis(x_data, y_data_05, y_data_2, y_data_3).render(), "StandardAnswer/task4/standard_answer_4.png")

第5关:Line:折线图(五)

from PreTest import *
from pyecharts import options as opts
from pyecharts.render import make_snapshot
from snapshot_phantomjs import snapshot
from pyecharts.commons.utils import JsCode

from pyecharts.charts import Line

js_formatter = """function (params) {
        console.log(params);
        return '降水量  ' + params.value + (params.seriesData.length ? ':' + params.seriesData[0].data : '');
    }"""


data_x_1 = [
    "2016-1",
    "2016-2",
    "2016-3",
    "2016-4",
    "2016-5",
    "2016-6",
    "2016-7",
    "2016-8",
    "2016-9",
    "2016-10",
    "2016-11",
    "2016-12",
]

data_x_2 = [
    "2015-1",
    "2015-2",
    "2015-3",
    "2015-4",
    "2015-5",
    "2015-6",
    "2015-7",
    "2015-8",
    "2015-9",
    "2015-10",
    "2015-11",
    "2015-12",
]


def line_chart() -> Line:
    # ********* Begin *********#  
    line = (
        Line(init_opts=opts.InitOpts(width="1600px", height="800px"))
    .add_xaxis(data_x_1)
    .extend_axis(
        xaxis_data=data_x_2,
        xaxis=opts.AxisOpts(
            type_="category",
            axistick_opts=opts.AxisTickOpts(is_align_with_label=True),
            axisline_opts=opts.AxisLineOpts(
                is_on_zero=False, linestyle_opts=opts.LineStyleOpts(color="#6e9ef1")
            ),
            axispointer_opts=opts.AxisPointerOpts(
                is_show=True, label=opts.LabelOpts(formatter=JsCode(js_formatter))
            ),
        ),
    )
    .add_yaxis(
        series_name="2015 降水量",
        is_smooth=True,
        symbol="emptyCircle",
        is_symbol_show=False,
        # xaxis_index=1,
        color="#d14a61",
        y_axis=[2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3],
        label_opts=opts.LabelOpts(is_show=False),
        linestyle_opts=opts.LineStyleOpts(width=2),
    )
    .add_yaxis(
        series_name="2016 降水量",
        is_smooth=True,
        symbol="emptyCircle",
        is_symbol_show=False,
        color="#6e9ef1",
        y_axis=[3.9, 5.9, 11.1, 18.7, 48.3, 69.2, 231.6, 46.6, 55.4, 18.4, 10.3, 0.7],
        label_opts=opts.LabelOpts(is_show=False),
        linestyle_opts=opts.LineStyleOpts(width=2),
    )
    .set_global_opts(
        legend_opts=opts.LegendOpts(),
        tooltip_opts=opts.TooltipOpts(trigger="none", axis_pointer_type="cross"),
        xaxis_opts=opts.AxisOpts(
            type_="category",
            axistick_opts=opts.AxisTickOpts(is_align_with_label=True),
            axisline_opts=opts.AxisLineOpts(
                is_on_zero=False, linestyle_opts=opts.LineStyleOpts(color="#d14a61")
            ),
            axispointer_opts=opts.AxisPointerOpts(
                is_show=True, label=opts.LabelOpts(formatter=JsCode(js_formatter))
            ),
        ),
        yaxis_opts=opts.AxisOpts(
            type_="value",
            splitline_opts=opts.SplitLineOpts(
                is_show=True, linestyle_opts=opts.LineStyleOpts(opacity=1)
            ),
        ),
    )

    )
    # ********** End **********#
    return line

make_snapshot(snapshot, line_chart().render("Result/render.html"), 'StudentAnswer/student_answer.png') # 输出图片
make_snapshot(snapshot, line_multiple_x_axis(data_x_1, data_x_2, js_formatter).render(), "StandardAnswer/task5/standard_answer_5.png")

第6关:Line:折线图(六)

from PreTest import *
from pyecharts import options as opts
from pyecharts.render import make_snapshot
from snapshot_phantomjs import snapshot

from pyecharts.charts import Line


x_data = [
    "00:00",
    "01:15",
    "02:30",
    "03:45",
    "05:00",
    "06:15",
    "07:30",
    "08:45",
    "10:00",
    "11:15",
    "12:30",
    "13:45",
    "15:00",
    "16:15",
    "17:30",
    "18:45",
    "20:00",
    "21:15",
    "22:30",
    "23:45",
]
y_data = [
    300,
    280,
    250,
    260,
    270,
    300,
    550,
    500,
    400,
    390,
    380,
    390,
    400,
    500,
    600,
    750,
    800,
    700,
    600,
    400,
]


def line_chart() -> Line:
    # ********* Begin *********#  
    line = (
        Line(init_opts=opts.InitOpts(width="1600px", height="800px"))
        .add_xaxis(x_data)
        .add_yaxis(
            series_name="用电量",
            y_axis=y_data,
            is_smooth=True,
            label_opts=opts.LabelOpts(is_show=False),
            linestyle_opts=opts.LineStyleOpts(width=2),
        )
        .set_global_opts(
            title_opts=opts.TitleOpts(title="一天用电量分布", subtitle="纯属虚构"),
            tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="cross"),
            xaxis_opts=opts.AxisOpts(boundary_gap=False),
            yaxis_opts=opts.AxisOpts(
                axislabel_opts=opts.LabelOpts(formatter="{value} W"),
                splitline_opts=opts.SplitLineOpts(is_show=True),
            ),
            visualmap_opts=opts.VisualMapOpts(
                is_piecewise=True,
                dimension=0,
                pieces=[
                    {"lte": 6, "color": "green"},
                    {"gt": 6, "lte": 8, "color": "red"},
                    {"gt": 8, "lte": 14, "color": "green"},
                    {"gt": 14, "lte": 17, "color": "red"},
                    {"gt": 17, "color": "green"},
                ],
            ),
        )
        .set_series_opts(
            markarea_opts=opts.MarkAreaOpts(
                data=[
                    opts.MarkAreaItem(name="早高峰", x=("07:30", "10:00")),
                    opts.MarkAreaItem(name="晚高峰", x=("17:30", "21:15")),
                ]
            )
        )
    )

    # ********** End **********#
    return line

make_snapshot(snapshot, line_chart().render("Result/render.html"), 'StudentAnswer/student_answer.png') # 输出图片
make_snapshot(snapshot, line_distribution_of_electricity(x_data, y_data).render(), "StandardAnswer/task6/standard_answer_6.png")

第7关:Line:折线图(七)

from PreTest import *
from pyecharts import options as opts
from pyecharts.render import make_snapshot
from snapshot_phantomjs import snapshot

from pyecharts.charts import Line

from pyecharts.faker import Faker

week_name_list = ["周一", "周二", "周三", "周四", "周五", "周六", "周日"]
high_temperature = [11, 11, 15, 13, 12, 13, 10]
low_temperature = [1, -2, 2, 5, 3, 2, 0]


def line_chart() -> Line:
    # ********* Begin *********#  
    line = (
        Line(init_opts=opts.InitOpts(width="1600px", height="800px"))
        .add_xaxis(xaxis_data=week_name_list)
        .add_yaxis(
            series_name="最高气温",
            y_axis=high_temperature,
            markpoint_opts=opts.MarkPointOpts(
                data=[
                    opts.MarkPointItem(type_="max", name="最大值"),
                    opts.MarkPointItem(type_="min", name="最小值"),
                ]
            ),
            markline_opts=opts.MarkLineOpts(
                data=[opts.MarkLineItem(type_="average", name="平均值")]
            ),
        )
        .add_yaxis(
            series_name="最低气温",
            y_axis=low_temperature,
            markpoint_opts=opts.MarkPointOpts(
                data=[opts.MarkPointItem(value=-2, name="周最低", x=1, y=-1.5)]
            ),
            markline_opts=opts.MarkLineOpts(
                data=[
                    opts.MarkLineItem(type_="average", name="平均值"),
                    opts.MarkLineItem(symbol="none", x="90%", y="max"),
                    opts.MarkLineItem(symbol="circle", type_="max", name="最高点"),
                ]
            ),
        )
        .set_global_opts(
            title_opts=opts.TitleOpts(title="未来一周气温变化", subtitle="纯属虚构"),
            tooltip_opts=opts.TooltipOpts(trigger="axis"),
            toolbox_opts=opts.ToolboxOpts(is_show=True),
            xaxis_opts=opts.AxisOpts(type_="category", boundary_gap=False),
        )

    )
    # ********** End **********#
    return line

make_snapshot(snapshot, line_chart().render("Result/render.html"), 'StudentAnswer/student_answer.png') # 输出图片
make_snapshot(snapshot, line_temperature_change(week_name_list, high_temperature, low_temperature).render(), "StandardAnswer/task7/standard_answer_7.png")

第8关:Line:折线图(八)

from PreTest import *
from pyecharts import options as opts
from pyecharts.render import make_snapshot
from snapshot_phantomjs import snapshot


from pyecharts.charts import Line

x_data = ["周一", "周二", "周三", "周四", "周五", "周六", "周日"]

y_data_1 = [120, 132, 101, 134, 90, 230, 210]
y_data_2 = [220, 182, 191, 234, 290, 330, 310]
y_data_3 = [150, 232, 201, 154, 190, 330, 410]
y_data_4 = [320, 332, 301, 334, 390, 330, 320]
y_data_5 = [820, 932, 901, 934, 1290, 1330, 1320]

def line_chart() -> Line:
    # ********* Begin *********#  
    line = (

        Line()
        .add_xaxis(xaxis_data=x_data)
        .add_yaxis(
            series_name="邮件营销",
            stack="总量",
            y_axis=[120, 132, 101, 134, 90, 230, 210],
            areastyle_opts=opts.AreaStyleOpts(opacity=0.5),
            label_opts=opts.LabelOpts(is_show=False),
        )
        .add_yaxis(
            series_name="联盟广告",
            stack="总量",
            y_axis=[220, 182, 191, 234, 290, 330, 310],
            areastyle_opts=opts.AreaStyleOpts(opacity=0.5),
            label_opts=opts.LabelOpts(is_show=False),
        )
            .add_yaxis(
            series_name="视频广告",
            stack="总量",
            y_axis=[150, 232, 201, 154, 190, 330, 410],
            areastyle_opts=opts.AreaStyleOpts(opacity=0.5),
            label_opts=opts.LabelOpts(is_show=False),
        )
        .add_yaxis(
            series_name="直接访问",
            stack="总量",
            y_axis=[320, 332, 301, 334, 390, 330, 320],
            areastyle_opts=opts.AreaStyleOpts(opacity=0.5),
            label_opts=opts.LabelOpts(is_show=False),
        )
        .add_yaxis(
            series_name="搜索引擎",
            stack="总量",
            y_axis=[820, 932, 901, 934, 1290, 1330, 1320],
            areastyle_opts=opts.AreaStyleOpts(opacity=0.5),
            label_opts=opts.LabelOpts(is_show=True, position="top"),
        )
        .set_global_opts(
            title_opts=opts.TitleOpts(title="堆叠区域图"),
            tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="cross"),
            yaxis_opts=opts.AxisOpts(
                type_="value",
                axistick_opts=opts.AxisTickOpts(is_show=True),
                splitline_opts=opts.SplitLineOpts(is_show=True),
            ),
            xaxis_opts=opts.AxisOpts(type_="category", boundary_gap=False),
        )
    
    )
    # ********** End **********#
    return line

make_snapshot(snapshot, line_chart().render("Result/render.html"), 'StudentAnswer/student_answer.png') # 输出图片
make_snapshot(snapshot, line_stacked_area(x_data, y_data_1, y_data_2, y_data_3, y_data_4, y_data_5).render(), "StandardAnswer/task8/standard_answer_8.png")

相关标签: echarts