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

PyEcharts 直角坐标系图表之柱状图/条形图

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

第1关:Bar:柱状图/条形图(一)

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

from pyecharts.charts import Bar

from pyecharts.faker import Faker
from pyecharts.globals import ThemeType

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


def bar_chart() -> Bar:
    # ********* Begin *********#  
    c = (
            Bar(init_opts=opts.InitOpts(theme=ThemeType.MACARONS))
            .add_xaxis(data_x)
            .add_yaxis("商家A",data_y_1)
            .add_yaxis("商家B",data_y_2,is_selected=False)
            .set_global_opts(
                title_opts=opts.TitleOpts(
                    title="Bar-基本示例",
                    subtitle="我也是通过 dict 进行配置的"),
                    )
    )
    # ********** End **********#
    return c

make_snapshot(snapshot, bar_chart().render("Result/render.html"), 'StudentAnswer/student_answer.png') # 输出图片
make_snapshot(snapshot, bar_base(data_x, data_y_1, data_y_2).render(), "StandardAnswer/task1/standard_answer_1.png")

第2关:Bar:柱状图/条形图(二)

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

from pyecharts.charts import Bar

x_data = [f"11月{str(i)}日" for i in range(1, 12)]
y_total = [0, 900, 1245, 1530, 1376, 1376, 1511, 1689, 1856, 1495, 1292]
y_in = [900, 345, 393, "-", "-", 135, 178, 286, "-", "-", "-"]
y_out = ["-", "-", "-", 108, 154, "-", "-", "-", 119, 361, 203]


def bar_chart() -> Bar:
    # ********* Begin *********#  
    bar = (
            Bar()
            .add_xaxis(x_data)
            .add_yaxis("",y_total,stack="总量",itemstyle_opts=opts.ItemStyleOpts(color="rgba(0,0,0,0)"),)
            .add_yaxis("收入",y_in,stack="总量")
            .add_yaxis("支出",y_out,stack="总量")
            .set_global_opts(
                yaxis_opts=opts.AxisOpts(type_='value'),
                    )

        )
    # ********** End **********#
    return bar

make_snapshot(snapshot, bar_chart().render("Result/render.html"), "StudentAnswer/student_answer.png") # 输出图片
make_snapshot(snapshot, bar_waterfall_plot(x_data, y_total, y_in, y_out).render(), "StandardAnswer/task2/standard_answer_2.png")

第3关:Bar:柱状图/条形图(三)

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

from pyecharts.charts import Bar

from pyecharts.faker import Faker

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


def bar_chart() -> Bar:
    # ********* Begin *********#  
    bar = (
        Bar()
        .add_xaxis(data_x)
        .add_yaxis("商家A",data_y_1)
        .add_yaxis("商家B",data_y_2)
        .set_global_opts(
            title_opts=opts.TitleOpts(title="Bar-显示 ToolBox"),
            toolbox_opts=opts.ToolboxOpts(),
            legend_opts=opts.LegendOpts(is_show=False),
        )
    )
    # ********** End **********#
    return bar

make_snapshot(snapshot, bar_chart().render("Result/render.html"), 'StudentAnswer/student_answer.png') # 输出图片
make_snapshot(snapshot, bar_toolbox(data_x, data_y_1, data_y_2).render(), "StandardAnswer/task3/standard_answer_3.png")

第4关:Bar:柱状图/条形图(四)

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

from pyecharts.charts import Bar

from pyecharts.faker import Faker

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


def bar_chart() -> Bar:
    # ********* Begin *********#  
    bar = (
        Bar()
        .add_xaxis(data_x)
        .add_yaxis("商家A", data_y_1)
        .add_yaxis(
            "商家B", 
            data_y_2,
            markpoint_opts=opts.MarkPointOpts(
                data=[
                    opts.MarkPointItem(
                    name="自定义标记点", 
                    coord=[data_x[2], data_y_2[2]], 
                    value=data_y_2[2]
                    )
                    ]
                ),
            )
        .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="平均值"),
                ]
            ),
        )
    )
    # ********** End **********#
    return bar

make_snapshot(snapshot, bar_chart().render("Result/render.html"), 'StudentAnswer/student_answer.png') # 输出图片
make_snapshot(snapshot, bar_mark(data_x, data_y_1, data_y_2).render(), "StandardAnswer/task4/standard_answer_4.png")

第5关:Bar:柱状图/条形图(五)

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

from pyecharts.charts import Bar

from pyecharts.faker import Faker

data_x = Faker.days_attrs
data_y = Faker.days_values
color = Faker.rand_color()


def bar_chart() -> Bar:
    # ********* Begin *********#  
    bar = (
        Bar()
        .add_xaxis(data_x)
        .add_yaxis("商家A",data_y,color=color)
        .set_global_opts(
            title_opts=opts.TitleOpts(title="Bar-DataZoom(slider+inside)"),
            datazoom_opts=[opts.DataZoomOpts(), opts.DataZoomOpts(type_="inside")], 
        )

    )
    # ********** End **********#
    return bar

make_snapshot(snapshot, bar_chart().render("Result/render.html"), 'StudentAnswer/student_answer.png') # 输出图片
make_snapshot(snapshot, bar_datazoom(data_x, data_y, color).render(), "StandardAnswer/task5/standard_answer_5.png")