代码之家  ›  专栏  ›  技术社区  ›  vferraz

大熊猫可旋转堆积面积图(Matplotlib)

  •  1
  • vferraz  · 技术社区  · 6 年前

    我有以下格式的数据

    import pandas as pd
    import matplotlib.pyplot as plt
    
        Metric  Country  Year    Value
    0       2G  Austria  2018  1049522
    1       2G  Austria  2019   740746
    2       2G  Austria  2020   508452
    3       2G  Austria  2021   343667
    4       2G  Austria  2022   234456
    65      3G  Austria  2018  2133823
    66      3G  Austria  2019  1406927
    67      3G  Austria  2020  1164042
    68      3G  Austria  2021  1043169
    69      3G  Austria  2022   920025
    130     4G  Austria  2018  7482733
    131     4G  Austria  2019  8551865
    132     4G  Austria  2020  8982975
    133     4G  Austria  2021  9090997
    134     4G  Austria  2022  8905121
    195     5G  Austria  2018        0
    196     5G  Austria  2019        0
    197     5G  Austria  2020    41995
    198     5G  Austria  2021   188848
    199     5G  Austria  2022   553826
    

    我正在尝试根据每年的值创建一个“面积”图表,并按度量分割。

    为此,我创建了一个用于聚合结果的透视表,如下所示:

    pivot_austria = pd.pivot_table(data_austria, index=['Metric'],
                                   columns=['Year'],
                                   values=['Value'], 
                                   aggfunc=sum, 
                                   fill_value=0)
    

    返回此格式的数据:

              Value                                    
    Year       2018     2019     2020     2021     2022
    Metric                                             
    2G      1049522   740746   508452   343667   234456
    3G      2133823  1406927  1164042  1043169   920025
    4G      7482733  8551865  8982975  9090997  8905121
    5G            0        0    41995   188848   553826
    

    但当我尝试plot命令时:

    plot = plt.stackplot(pivot_austria.columns, pivot_austria.values, labels = pivot_austria.index)
    

    我出错了

        return np.array(data, dtype=np.unicode)
    
    ValueError: setting an array element with a sequence
    

    我尝试了很多方法来设计这个,不管有没有轴,到目前为止它还没有起作用,有人知道我可能做错了什么吗?

    2 回复  |  直到 6 年前
        1
  •  3
  •   Scott Boston    6 年前

    我不确定您要生成哪种类型的图,但删除该值周围的背景会有所帮助。

    我们先试试这个:

    pivot_austria=pd.pivot_table(data_austria,index=['metric'],
    列=[“年”],
    values='value',
    aggfunc=总和,
    填充值=0)
    
    plt.stackplot(pivot_austria.columns,pivot_austria.values,labels=pivot_austria.index)
    ax=plt.gca()。
    ax.set_xticks(Pivot_Australia.Columns)
    

    输出:

    或者正如@pask在他的解决方案中建议的那样,让熊猫来处理:

    ax=pivot_austria.plot.area())
    ax.set_xticks(Pivot_Australia.index)
    

    输出:

    编辑显示为百分比:

    ax=(pivot_austria/pivot_austria.sum(1.max()).plot.area())
    ax.set_xticks(Pivot_Australia.index)
    ax.set ytickLabels([':,.2%'。在ax.get yticks()]中为x设置(x)格式)
    最大设置值(0,1)
    
    
    

    输出:

    输出:

    enter image description here

    或者正如@pask在他的解决方案中建议的,让熊猫来处理它:

    ax = pivot_austria.plot.area()
    ax.set_xticks(pivot_austria.index)
    

    输出:

    enter image description here

    编辑显示为百分比:

    ax = (pivot_austria / pivot_austria.sum(1).max()).plot.area()
    ax.set_xticks(pivot_austria.index)
    ax.set_yticklabels(['{:,.2%}'.format(x) for x in ax.get_yticks()])
    ax.set_ylim(0,1)
    

    输出:

    enter image description here

        2
  •  2
  •   pask    6 年前

    熊猫已经包括了一种简单的绘制区域图的方法。

    尝试:

    pivot_austria.T.plot.area(xticks=pivot_austria.T.index)