数据分析读书

1.pyecharts柱状图

2021-08-16  本文已影响0人  无聊的兔子

一、适用条件
1、柱状图:数据集不宜过大
2、堆积柱状图:两者可以合起来看总量、各部分对比
3、组合柱状图:可以将比例放在副坐标轴、看趋势
二、代码实现
1.导入所需包

from pyecharts import options as opts
from pyecharts.charts import Bar, Line
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
import pandas as pd
import numpy as np 

2.数据整理

###导入数据
df = pd.read_excel('picture.xlsx',sheet_name='bar')
###观察数据
print(df.head())
###选择所需数据
df1 = df[['国家','成单量','退单']]
df_country = df1.groupby(by = ["国家"]).sum().reset_index().sort_values(ascending=False,by='成单量')###sort_value 可以按照大小进行排序
# print(df_country["国家"])
#print(list(df_country["成单id"]))
x_list = list(df_country["国家"])
y_list = list(df_country["成单量"])
y1_list = list(df_country["退单"])
ratio_list = list(round((df_country["退单"]/df_country["成单量"])*100,2))
title1 = "国家成单退单统计"
subtitle1 = "子标题"

3.1双柱状图

###画图
def bar_chart() -> Bar:
    ################### 这部分可以直接用,保存成网页
    c = (
        Bar(init_opts=opts.InitOpts(theme=ThemeType.VINTAGE))###括号内设置主题风格
        .add_xaxis(x_list)
        .add_yaxis("成单量",y_list) 
        .add_yaxis("退单", y1_list)
        .reversal_axis() ####翻转x,y轴
        .set_global_opts(
            xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=-15)), ###设置旋转角度问题
            title_opts=opts.TitleOpts(title= title1, subtitle= subtitle1), ###设置标题
            brush_opts=opts.BrushOpts(),
        )
        .set_series_opts(label_opts=opts.LabelOpts(is_show=True))   ###设置标签展示
        # .render("1.html") ###保存成网页
    ) 
    ###################
    return c
make_snapshot(snapshot, bar_chart().render(), "1.gif")
if __name__ == '__main__':
    bar_chart()
1.gif

3.2堆积柱状图

c = (
    Bar(init_opts=opts.InitOpts(theme=ThemeType.VINTAGE))###括号内设置主题风格
    .add_xaxis(x_list)
    .add_yaxis("成单量",y_list,stack="stack1") 
    .add_yaxis("退单", y1_list,stack="stack1")
    .set_global_opts(
        xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=-15)), ###设置旋转角度问题
        title_opts=opts.TitleOpts(title= title1, subtitle= subtitle1), ###设置标题
        brush_opts=opts.BrushOpts(),
        toolbox_opts=opts.ToolboxOpts(), ####渲染成网页时,直接下载
        datazoom_opts=opts.DataZoomOpts(), ####可拉动x轴
    )
    .set_series_opts(
        label_opts=opts.LabelOpts(is_show=True, ###设置标签展示
            position="right",
            formatter=JsCode(
                "function(x){return Number(x.data).toFixed();}"
            ),
        ) 
    )
        .render("1.html") ###保存成网页
) 
2.gif

3.3组合柱状图

c = (
    Bar(init_opts=opts.InitOpts(theme=ThemeType.VINTAGE))###括号内设置主题风格
    .add_xaxis(x_list)
    .add_yaxis(
        "成单量",
        y_list,
        label_opts=opts.LabelOpts(is_show=False),
    ) 
    .add_yaxis(
        "退单", 
        y1_list,
        label_opts=opts.LabelOpts(is_show=False),
    )
    .extend_axis(
        yaxis=opts.AxisOpts(
            name="比率",
            type_="value",
            min_=0,
            max_=10,
            interval=2.5,
            axislabel_opts=opts.LabelOpts(formatter="{value}%"),
        )
    )
    .set_global_opts(
        tooltip_opts=opts.TooltipOpts(
        is_show=True, trigger="axis", axis_pointer_type="cross"
        ),
         xaxis_opts=opts.AxisOpts(
            type_="category",
            axispointer_opts=opts.AxisPointerOpts(is_show=True, type_="shadow"),
        ),
        yaxis_opts=opts.AxisOpts(
            name="单量",
            type_="value",
            min_=0,
            max_=1500,
            interval=300,
            axislabel_opts=opts.LabelOpts(formatter="{value} rmb"),
            axistick_opts=opts.AxisTickOpts(is_show=True),
            splitline_opts=opts.SplitLineOpts(is_show=True),
        ),
        title_opts=opts.TitleOpts(title= title1, subtitle= subtitle1), ###设置标题
        brush_opts=opts.BrushOpts(),
        toolbox_opts=opts.ToolboxOpts(), ####渲染成网页时,直接下载
        datazoom_opts=opts.DataZoomOpts(), ####可拉动x轴
    )
)   
line = (
    Line(init_opts=opts.InitOpts(theme=ThemeType.VINTAGE))
    .add_xaxis(xaxis_data=x_list)
    .add_yaxis(
        series_name="比率",
        yaxis_index=1,
        y_axis=ratio_list,
        label_opts=opts.LabelOpts(is_show=True),
    )
)

c.overlap(line).render("1.html")
国家成单退单统计 (1).png
上一篇下一篇

猜你喜欢

热点阅读