Skip to content Skip to sidebar Skip to footer

Python 3.x - Horizontal Bar Plot

I have a dataframe df - df- Source Amount 1 University of Minnesota 119367000 2 Minnesota State Colleges and Universities 159812000 3 Education 7491000 4 Minneso

Solution 1:

I think you can use plot.barh, but before set_index with rename_axis (new in pandas0.18.0) and sort_values:

#set index from column Source, remove index namedf = df.set_index('Source').rename_axis(None)
#sorting valuesdf = df.sort_values('Amount', ascending=False)
print df
                                                    Amount
TOTAL                                            893054000
Bond Proceeds Fund (General Fund Debt Service)   814745000
Minnesota State Colleges and Universities        159812000
Administration                                   127000000
University of Minnesota                          119367000
Employment and Economic Development               92130000
Human Services                                    86387000
Natural Resources                                 63480000
Transportation                                    57263000Public Facilities Authority                       45993000
Metropolitan Council                              45968000
Bond Proceeds Fund (User Financed Debt Service)   39104000
State Transportation Fund                         36613000
Housing Finance Agency                            20000000
Minnesota Historical Society                      12002000
Zoological Garden                                 12000000
Corrections                                       11881000
Minnesota State Academies                         11354000
Bond Proceeds Cancellations                       10849000
Cancellations                                     10849000
Board of Water and Soil Resources                  8000000
Minnesota Amateur Sports Commission                7973000
Trunk Highway Fund                                 7950000
Education                                          7491000
Maximum Effort School Loan Fund                    5491000Public Safety                                      4030000
Military Affairs                                   3244000
Veterans Affairs                                   2800000
Pollution Control Agency                           2625000
Perpich Center for Arts Education                  2000000
Bond Sale Expenses                                  900000
Agriculture                                         203000
df.plot.barh(figsize=(10,20))
plt.show()

graph

Solution 2:

Boilerplate

In [1]: import matplotlib.pyplotas plt

In [2]: %matplotlib
Using matplotlib backend: Qt4AggIn [3]: import pandas as pd

My fake data

In [4]: data = pd.read_csv('data.csv')

In [5]: data
Out[5]: 
       Name   Value
0  asde rty     10014 wewer     2002   uwei ef     300

Now, the interesting part, first use dataframe methods to plot dataframe content,

In [6]: data.plot.barh()
Out[6]: <matplotlib.axes._subplots.AxesSubplot at 0x7facb0706198>

The above labels y-axis with 0, 1, 2, no good... so we have to modify the plotted object, first you have to grab the plotted object (gca stands for get current axis)

In [7]: ax = plt.gca()

then you tell, it's object oriented isn't it? you tell to the current axis to modify the y ticks labels, that is (unsurprisingly)

In [8]: ax.set_yticklabels(data['Name']);
Out[8]: 

In [9]: 

and this is the output

enter image description here

Solution 3:

I might be embarrassing myself here, but could it be that you need to feed a different type of data into matplotlib?

import matplotlib.pyplot as pltexpense_df= {'Amount' : 0, 'Amount' : 1, 'Amount' : 2}
expense = {'Source' : 1, 'Source' : 2, 'Source' : 3}

plt.barh(expense_df['Amount'],expense['Source'])
plt.show()

Solution 4:

The second argument to plt.barh() must be numerical and it does not look like expense['Source'] is. I cannot provide a more concrete answer since I don't know what you want the widths of the bars to be, but obviously "University of Minnesota" is not a valid width.

Post a Comment for "Python 3.x - Horizontal Bar Plot"