Pandas DataFrame 条形图与其他列的 sort_values

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40902958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:33:28  来源:igfitidea点击:

Pandas DataFrame bar plot with sort_values by other column

pythonpandasmatplotlibplot

提问by running man

I have a Pandas DataFrame. I want to plot two columns' values with bar plot, and the bar plot sorts values by the other column.

我有一个 Pandas DataFrame。我想用条形图绘制两列的值,条形图按另一列对值进行排序。

For example, I want to sort values in descending order by column a_b(sum of column aand b). In addition, the xlabel is rotated, I want to fix it.

例如,我想按列a_b(列a和 的总和b)按降序对值进行排序。另外,xlabel旋转了,我想修复它。

Your help would be appreciated.

您的帮助将不胜感激。

import pandas as pd
%matplotlib inline
a = pd.Series([4,8,6,7,8,3,9,7])
b = pd.Series([3,6,8,3,4,6,10,4])
a_b = a+b
df = pd.concat([a,b,a_b],axis=1,join='inner')
df.columns = ['a','b','c']

df[['a','b']].sort_values(by='a',ascending=False).plot(kind='bar',stacked=True)

enter image description here

在此处输入图片说明

采纳答案by piRSquared

Sort dataframe first by cthen plot with.

先对数据框进行排序,c然后再绘制。

df.sort_values('c', ascending=False)[['a','b']].plot.bar(stacked=True)

enter image description here

在此处输入图片说明

回答by qmaruf

Fix the rotation problem using rot=0in @piRSquared answer.

使用rot=0@piRSquared 答案修复旋转问题。

df.sort_values('c', ascending=False)[['a','b']].plot.bar(stacked=True, rot=0)

enter image description here

在此处输入图片说明