2

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

Bar plot

  • 2
    I genuinely don't understand your question. Firstly, you don't provide a [minimal, complete and verifiable example](https://stackoverflow.com/help/mcve). Lots of unnecessary code here and still we can't simply copy it to reproduce your problem. Secondly, you are aware of two simple solutions, but want another one? Why? – Mr. T Mar 17 '18 at 10:47
  • There are three options: (1) use pandas, (2) update matplotlib (3) use numeric ticks. If you need a different solution the question would need to make the motivation clear why none of those can be used. – ImportanceOfBeingErnest Mar 17 '18 at 11:17

0 Answers0