![](/img/trans.png)
[英]How to plot the rotation of a curve along the x-axis with matplotlib in Python?
[英]How to plot grouped columns along the x-axis in Matplotlib?
我有一个三列的 dataframe。 City
, State
, Sales
。
要检索我使用的每个城市的总Sales
:
df.groupby(['City','State']).sum()[['Sales']]
因为我必须能够区分同名城市,所以也需要包括State
。 现在,我的问题是,如何将City
和State
沿 x 轴分组,并将Sales
放在 Matplotlib 条形图的 y 轴上? 我可以将City
和State
到它自己的列中,然后将 plot 连接到 x 轴上的新列中。 我觉得这有点草率。 将City
和State
放在 x 轴上的代码是什么样的(不创建新列)。 我希望它看起来像下面的图片。 我很感激帮助。 提前致谢。
创建一个列名,用str,然后groupby,然后plot,就这么简单......
df['Name'] = df['City'] + ' (' + df['State']) + ')'
df.groupby(['Name']).sum()[['Sales']].plot()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.