数据分析读书

2.pyecharts折线图

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

一、适用条件
1、折线图:主要展示的是一种趋势变化关系,一个连续的时间间隔内的数据变化;
2、平滑折线图:数据反差过大时,让折现不生硬;
3、堆积折线图:显示每一数值所占大小随时间或有序类别而变化的趋势;
4、堆积面积折线图:显示每一数值所占百分比随时间或有序类别而变化的趋势;
二、代码实现
1.导入所需包

from numpy.lib import index_tricks
from pyecharts import options as opts
from pyecharts.charts import  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='line')
###观察数据
print(df.head())
###选择所需数据
my_type = pd.CategoricalDtype(
  categories=['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月'],
  ordered=True
) ###########按照规定索引重新排序
df1 = df[['月份','商家','销量']]
df_country = df1.groupby(by = ["月份"]).sum().reset_index()
pt = pd.pivot_table(df, index=['月份'], columns=['商家'], values=['销量'], aggfunc={'销量':np.sum}, margins=False).reset_index()
df_country["月份"] = df_country["月份"].astype(my_type)############
pt["月份"] = pt["月份"].astype(my_type)###########
# print(pt.columns)
df_country = df_country.sort_values("月份")#########
pt = pt.sort_values("月份")########
x_list = list(df_country["月份"])
y_list = list(df_country["销量"])
y1_list = list(pt['销量',"A"])
y2_list = list(pt['销量',"B"])
y3_list = list(pt['销量',"C"])
y4_list = list(pt['销量',"D"])
y5_list = list(pt['销量',"E"])
title1 = "销量统计"
subtitle1 = "纯属虚构"

3.1折线图

def line_chart() -> Line:
    ################### 这部分可以直接用,保存成网页
    c = (
        Line()
        .add_xaxis(xaxis_data=x_list) ###设置x轴
        .add_yaxis(
            series_name="所有商家",###图例名字 
            y_axis= y_list, ###值
            label_opts=opts.LabelOpts(is_show= True), ###是否显示值
            is_smooth =  True,# 是否平滑曲线         
            is_step = False, # 是否显示成阶梯图
            is_hover_animation = True,    # 是否开启 hover 在拐点标志上的提示动画效果。
        )
        .add_yaxis(
            series_name="商家E",
            y_axis=y5_list, 
            label_opts=opts.LabelOpts(is_show= True), ###是否显示标签
            is_smooth =  True,# 是否平滑曲线         
            is_step = False, # 是否显示成阶梯图
            is_hover_animation = True,    # 是否开启 hover 在拐点标志上的提示动画效果。

        )
        .set_global_opts(
            title_opts=opts.TitleOpts(title=title1, subtitle=subtitle1),
            tooltip_opts=opts.TooltipOpts(trigger="axis"),
            toolbox_opts=opts.ToolboxOpts(is_show=True),
            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),
        )
        # .render("1.html")
    )
    return c
make_snapshot(snapshot, line_chart().render(), "2_2.gif")
if __name__ == '__main__':
    line_chart()
2_2.gif

3.2&3.3堆积面积折线图

(
    Line()
    .add_xaxis(xaxis_data=x_list) ###设置x轴
    .add_yaxis(
        series_name="所有商家",###图例名字 
        y_axis= y_list, ###值
        label_opts=opts.LabelOpts(is_show=False), ###是否显示值
        is_smooth =  True,# 是否平滑曲线         
        is_step = False, # 是否显示成阶梯图
        is_hover_animation = True,    # 是否开启 hover 在拐点标志上的提示动画效果。
    )
    .add_yaxis(
        series_name="商家A",
        stack="总销量",   ###设置堆叠折线图
        y_axis=y1_list,
        label_opts=opts.LabelOpts(is_show=False),
        is_smooth = True,
    )
    .add_yaxis(
        series_name="商家B",
        stack="总销量", ###设置堆叠折线图
        y_axis=y2_list,
        label_opts=opts.LabelOpts(is_show=False),
        is_smooth = True,
    )
    .add_yaxis(
        series_name="商家C",
        stack="总销量", ###设置堆叠折线图
        y_axis=y3_list,
        label_opts=opts.LabelOpts(is_show=False),
        is_smooth = True,
    )
    .add_yaxis(
        series_name="商家D",
        stack="总销量", ###设置堆叠折线图
        y_axis=y4_list,
        label_opts=opts.LabelOpts(is_show=False),
        is_smooth = True,
    )
    .add_yaxis(
        series_name="商家E",
        stack="总销量",  ###设置堆叠折线图
        y_axis=y5_list, 
        label_opts=opts.LabelOpts(is_show=False), ###是否显示标签
        is_smooth =True,# 是否平滑曲线         
        is_step = False, # 是否显示成阶梯图
        is_hover_animation = True,    # 是否开启 hover 在拐点标志上的提示动画效果。

    )
    .set_global_opts(
        title_opts=opts.TitleOpts(title=title1, subtitle=subtitle1),
        tooltip_opts=opts.TooltipOpts(trigger="axis"),
        toolbox_opts=opts.ToolboxOpts(is_show=True),
        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),
    )
    .set_series_opts(   ###设置面积图
        areastyle_opts=opts.AreaStyleOpts(opacity=0.5),
        label_opts=opts.LabelOpts(is_show=False),
    )
    .render("3.html")
)
2_1.gif
上一篇 下一篇

猜你喜欢

热点阅读