I have a dataframe with words as index, I'm ordering the DF by a column. Now when I try to plot it with pyplot it always order the x-axis alphabetically as opposed to the behaviour of pandas' plot or seaborns. Is there any other way of keeping my order in plt than going to the trick of using numbers to order it the way I want and then change the labels or not using plt? (That was the solution here for example, but the common answer is to use df.plot which I know that works)
As you can see in the bar plot, plt take into account the order provided to set the color scale but then reorder the columns to be alphabetical.
This problem happened to me not only in the case but everytime I try to use a string column as x-axis.
In [95]:
income2012g10k = income2012[income2012['Income'] > 10000]
grouped10kC = income2012g10k.groupby('Region').count()
groupedC = income2012.groupby('Region').count()
groupedC['Income'] = grouped10kC['Income']
groupedC['Proportion'] = groupedC['Income']/groupedC['Country']
groupedC.sort_values('Proportion', inplace=True)
plt.figure(figsize=(12,4))
plt.bar(groupedC.index,groupedC['Proportion']
,color=sns.color_palette('Blues'))
sns.despine()
groupedC
Out[95]:
Country Income Proportion
Region
AFRICA 50 10 0.200000
OCEANIA 13 4 0.307692
ASIA 37 21 0.567568
NORTH AMERICA 20 13 0.650000
SOUTH AMERICA 12 9 0.750000
EUROPE 44 37 0.840909