一、适用条件
1、箱形图:观察数据的中位数、尾长、异常值、分布区间等形状,大致了解数据结构。
二、代码实现
1.导入所需包
from pandas.core.indexes.base import Index
import pyecharts.options as opts
from pyecharts.charts import Boxplot
import pandas as pd
import numpy as np
from pyecharts.commons.utils import JsCode
from pyecharts.globals import ThemeType
from pyecharts.render import make_snapshot
#from snapshot_phantomjs import snapshot
from snapshot_pyppeteer import snapshot
2.数据整理
###导入数据
df = pd.read_excel('picture.xlsx',sheet_name='boxplot')
###观察数据
df1 = df.loc[df["商家"]=="A"]
df2 = df.loc[df["商家"]=="B"]
# print(df.head())
v1 = [list(df1["1月"]),list(df1["2月"]),list(df1["3月"]),list(df1["4月"]),list(df1["5月"]),list(df1["6月"])]
v2 = [list(df2["1月"]),list(df2["2月"]),list(df2["3月"]),list(df2["4月"]),list(df2["5月"]),list(df2["6月"])]
x_list = list(df)[1:]
3 箱线图
def Boxplot_chart() -> Boxplot:
################## 这部分可以直接用,保存成网页
c =(
Boxplot()
.add_xaxis(x_list)
.add_yaxis("A", (v1))
.add_yaxis("B", (v2))
.set_global_opts(
title_opts=opts.TitleOpts(title="BoxPlot-基本示例"),
toolbox_opts=opts.ToolboxOpts(is_show=True),
)
# .render("1.html")
)
return c
make_snapshot(snapshot, Boxplot_chart().render(), "7_1.gif")
if __name__ == '__main__':
Boxplot_chart()
7_1.gif
网友评论