简体   繁体   English

Pandas DataFrame条形图,其他列的sort_values

[英]Pandas DataFrame bar plot with sort_values by other column

I have a Pandas DataFrame. 我有一个Pandas DataFrame。 I want to plot two columns' values with bar plot, and the bar plot sorts values by the other column. 我想用条形图绘制两列的值,条形图按另一列对值进行排序。

For example, I want to sort values in descending order by column a_b (sum of column a and b ). 例如,我想按列a_b (列ab总和)按降序对值进行排序。 In addition, the xlabel is rotated, I want to fix it. 另外,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)

在此输入图像描述

Sort dataframe first by c then plot with. 首先按c对数据帧排序然后用。

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

在此输入图像描述

Fix the rotation problem using rot=0 in @piRSquared answer. 在@piRSquared答案中使用rot=0修复旋转问题。

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

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM