Sinchetru Sinchetru - 14 days ago 13
Python Question

How to sort values in pandas pivot table and plot them?

I have a pivot table (i.e):

City Atlanta New York Chicago
Region name Slow Grid Fathe

2010-01 1 2 3
2010-02 3 15 23
... ...
2016-01 12 1 0


when I try to plot some values with the following:

pivot.ix['2016-01'].plot(kind='barh',
figsize=(7, 10),
width=0.8,
fontsize=10,
colormap='autumn')


I get the following graph:
enter image description here

How to change the code to plot this graph ascendingly?

Answer

Adding .sort_values() in between the pivot.ix[] and the .plot() calls will sort the Series returned by .ix[] before it's plotted and should give you the result you want.

pivot.ix['2016-01'].sort_values().plot(kind='barh', 
                                       figsize=(7, 10), 
                                       width=0.8, 
                                       fontsize=10, 
                                       colormap='autumn')
Comments