Python - Using a List, Dict Comprehension, and Mapping to Change Plot Order
Question: I am relatively new to Python, Pandas, and plotting. I am looking to make a
custom sort order in a pandas plot using a list, mapping, and sending them
through to the plot function.

I am not "solid" on mapping or dict comprehensions. I've looked around a bit
on Google and haven't found anything really clear - so any direction to
helpful references would be much appreciated.

I have a dataframe that is the result of a groupby:

    
    
    Exchange
    AMEX       267
    NYSE      2517
    Nasdaq    2747
    Name: Symbol, dtype: int64
    

The numerical column is 'Symbol' and the exchange listing is the index

When I do a straightforward pandas plot

    
    
    my_plot = Exchange['Symbol'].plot(kind='bar')
    

I get this:

[![enter image description
here](http://i.stack.imgur.com/hcNlq.jpg)](http://i.stack.imgur.com/hcNlq.jpg)

The columns are in the order of the rows in the dataframe (Amex, NYSE, Nasdaq)
but I would like to present, left to right, NYSE, Nasdaq, and Amex. So a
"sort" won't work.

There is another post:

[Sorting the Order of
Bars](http://stackoverflow.com/questions/22635110/sorting-the-order-of-bars-
in-pandas-matplotlib-bar-plots)

that gets at this - but I just couldn't figure it out.

I feel like the solution is one step out of my reach. I think this is a very
important concept to get down as it would help me considerably in visualizing
data where the not-infrequent case of a custom row presentation in a chart is
needed. I'm also hoping discussion here could help me better understand
mapping as that seems to be very useful in many instances but I just can't
seem to find the right on-line resource to explain it clearly.

Thank you in advance.


Answer: The solution to your problem is putting your output dataframe into desired
order:

    
    
    order = [1,2,0] # the desired order
    Exchange['Symbol'].iloc[order]
    
    NYSE      2517
    Nasdaq    2747
    AMEX       267
    Name: Symbol, dtype: int64
    

As soon as you have the rightly ordered data you can plot it:

    
    
    Exchange['Symbol'].iloc[order].plot(kind='bar');
    

[![enter image description
here](http://i.stack.imgur.com/aY8Zn.png)](http://i.stack.imgur.com/aY8Zn.png)

