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
Pandas DataFrame bar plot with sort_values by other column
提问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 a
and 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)