第1关:Boxplot:箱形图(一)
from PreTest import *
from pyecharts import options as opts
from pyecharts.render import make_snapshot
from snapshot_phantomjs import snapshot
from pyecharts.charts import Boxplot
data1 = [
[850, 740, 900, 1070, 930, 850, 950, 980, 980, 880, 1000, 980],
[960, 940, 960, 940, 880, 800, 850, 880, 900, 840, 830, 790],
]
data2 = [
[890, 810, 810, 820, 800, 770, 760, 740, 750, 760, 910, 920],
[890, 840, 780, 810, 760, 810, 790, 810, 820, 850, 870, 870],
]
def boxplot_chart() -> Boxplot:
# ********* Begin *********#
boxplot = (
Boxplot()
.add_xaxis(["expr1", "expr2"])
.add_yaxis("A",Boxplot.prepare_data(data1))
.add_yaxis("B",Boxplot.prepare_data(data2))
.set_global_opts(
title_opts=opts.TitleOpts(title="BoxPlot-基本示例")
)
)
# ********** End **********#
return boxplot
make_snapshot(snapshot, boxplot_chart().render("Result/render.html"), 'StudentAnswer/student_answer.png') # 输出图片
make_snapshot(snapshot, boxplot_base(data1, data2).render(), "StandardAnswer/task1/standard_answer_1.png")
?第2关:Boxplot:箱形图(二)
from PreTest import *
from pyecharts import options as opts
from pyecharts.render import make_snapshot
from snapshot_phantomjs import snapshot
from pyecharts.charts import Boxplot
from pyecharts.commons.utils import JsCode
import json
with open("data.json", "r", encoding="utf-8") as f:
data = json.load(f)
axis_data = [
"0",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"11",
"12",
"13",
"14",
"15",
"16",
"17",
]
def boxplot_chart() -> Boxplot:
# ********* Begin *********#
boxplot = (
Boxplot(init_opts=opts.InitOpts(width="1600px", height="800px"))
.add_xaxis(xaxis_data=axis_data)
.add_yaxis(
series_name="category0",
y_axis=data[0]["boxData"],
tooltip_opts=opts.TooltipOpts(
formatter=JsCode(
"""function(param) { return [
'Experiment ' + param.name + ': ',
'upper: ' + param.data[0],
'Q1: ' + param.data[1],
'median: ' + param.data[2],
'Q3: ' + param.data[3],
'lower: ' + param.data[4]
].join('<br/>') }"""
)
),
)
.add_yaxis(
series_name="category1",
y_axis=data[1]["boxData"],
tooltip_opts=opts.TooltipOpts(
formatter=JsCode(
"""function(param) { return [
'Experiment ' + param.name + ': ',
'upper: ' + param.data[0],
'Q1: ' + param.data[1],
'median: ' + param.data[2],
'Q3: ' + param.data[3],
'lower: ' + param.data[4]
].join('<br/>') }"""
)
),
)
.add_yaxis(
series_name="category2",
y_axis=data[2]["boxData"],
tooltip_opts=opts.TooltipOpts(
formatter=JsCode(
"""function(param) { return [
'Experiment ' + param.name + ': ',
'upper: ' + param.data[0],
'Q1: ' + param.data[1],
'median: ' + param.data[2],
'Q3: ' + param.data[3],
'lower: ' + param.data[4]
].join('<br/>') }"""
)
),
)
.set_global_opts(
title_opts=opts.TitleOpts(title="Multiple Categories", pos_left="center"),
legend_opts=opts.LegendOpts(pos_top="3%"),
tooltip_opts=opts.TooltipOpts(trigger="item", axis_pointer_type="shadow"),
xaxis_opts=opts.AxisOpts(
name_gap=30,
boundary_gap=True,
splitarea_opts=opts.SplitAreaOpts(
areastyle_opts=opts.AreaStyleOpts(opacity=1)
),
axislabel_opts=opts.LabelOpts(formatter="expr {value}"),
splitline_opts=opts.SplitLineOpts(is_show=False),
),
yaxis_opts=opts.AxisOpts(
type_="value",
min_=-400,
max_=600,
splitarea_opts=opts.SplitAreaOpts(is_show=False),
),
datazoom_opts=[
opts.DataZoomOpts(type_="inside", range_start=0, range_end=20),
opts.DataZoomOpts(type_="slider", xaxis_index=0, is_show=True),
],
))
# ********** End **********#
return boxplot
make_snapshot(snapshot, boxplot_chart().render("Result/render.html"), "StudentAnswer/student_answer.png") # 输出图片
make_snapshot(snapshot, boxplot_multiple(axis_data, data).render(), "StandardAnswer/task2/standard_answer_2.png")
|