首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >来自Pandas pivot的堆积面积图(matplotlib)

来自Pandas pivot的堆积面积图(matplotlib)
EN

Stack Overflow用户
提问于 2018-07-18 21:03:47
回答 2查看 587关注 0票数 2

我有以下格式的数据

代码语言:javascript
运行
复制
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

我正在尝试创建一个基于每年的值的“面积”图,按指标划分。

为此,我创建了一个数据透视表来聚合结果,如下所示:

代码语言:javascript
运行
复制
pivot_austria = pd.pivot_table(data_austria, index=['Metric'],
                               columns=['Year'],
                               values=['Value'], 
                               aggfunc=sum, 
                               fill_value=0)

它以这种格式返回数据:

代码语言:javascript
运行
复制
          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命令时:

代码语言:javascript
运行
复制
plot = plt.stackplot(pivot_austria.columns, pivot_austria.values, labels = pivot_austria.index)

我得到一个错误

代码语言:javascript
运行
复制
    return np.array(data, dtype=np.unicode)

ValueError: setting an array element with a sequence

我尝试了很多方法来绘制这个图,不管有没有轴心,到目前为止它都不起作用,有人知道我会做错什么吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-07-18 21:31:09

我不确定您想要生成哪种类型的图,但去掉值周围的包围会有所帮助。

让我们先试试这个:

代码语言:javascript
运行
复制
pivot_austria = pd.pivot_table(data_austria, index=['Metric'],
                               columns=['Year'],
                               values='Value', 
                               aggfunc=sum, 
                               fill_value=0)

plt.stackplot(pivot_austria.columns, pivot_austria.values, labels = pivot_austria.index)
ax = plt.gca()
ax.set_xticks(pivot_austria.columns)

输出:

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

代码语言:javascript
运行
复制
ax = pivot_austria.plot.area()
ax.set_xticks(pivot_austria.index)

输出:

编辑以显示为百分比:

代码语言:javascript
运行
复制
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)

输出:

票数 5
EN

Stack Overflow用户

发布于 2018-07-18 21:31:13

Pandas已经提供了一种简单的绘制面积图的方法

尝试:

代码语言:javascript
运行
复制
pivot_austria.T.plot.area(xticks=pivot_austria.T.index)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51402760

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档