A Horizontal Bar Graph In Python

Hi there. In this short page, I play around with Python and the matplotlib module in making a sorted horizontal bar graph. Getting the basics of the bar graph is not too hard but adding in the nicer details does take a bit of work.

The Python modules that are imported are pandas and matplotlib.

 


The Sample Data

 

To make things simple, a (fake) survey asking for the favourite colour of students is conducted. There were eight colours to choose from and each colour has an associated count with it.

 

A pandas dataframe is created which uses the colours and the counts (y). This dataframe will be sorted from highest to lowest in terms of counts. Sorting the dataframe will allow for a sorted bar graph.

 

The sorted dataframe looks like this:

 

 


Creating A Horizontal Bar Graph

 

To start off, a custom font can be chosen instead of the default font for plotting with matplotlib. I have chosen the Comic Sans MS font which will be used in the title and labels.

 

The bulk of the graph comes from the following code. After fig, ax = plt.subplots(), the ax.barh() will allow for horizontal bars in the bar graph. The letter h in barh is horizontal. The arguments in ax.barh() are y_position for the colour names, sorted[“Count”] which is the column of counts in the sorted dataframe, align = “center”, an HTML colour code for the bars and ecolor = “black”. For colours, I use the HTML colour codes but you can use common colours such as red, green, blue and so on.

 

 

To make the graph look nicer we need to add a title and labels. In the labels functions and in the title function associated with ax, the **csfont part is added as an additional argument. This will allow the Comic Sans MS font to override the default font.

 

When it comes to bars, it may be hard for the viewer to tell what exactly the count is. Including text labels of counts helps the viewer and eliminates guesswork. To add the labels a for loop is used along with enumerate() and ax.text().

 

To show the plot, use plt.show().

 

 

 


References

 

  • https://stackoverflow.com/questions/28022227/sorted-bar-charts-with-pandas-matplotlib-or-seaborn
  • https://stackoverflow.com/questions/21487329/add-x-and-y-labels-to-a-pandas-plot
  • https://stackoverflow.com/questions/20600006/simple-customization-of-matplotlib-pandas-bar-chart-labels-ticks-etc
  • Python For Data Visualization By Igor Milovanovic
  • https://stackoverflow.com/questions/20600006/simple-customization-of-matplotlib-pandas-bar-chart-labels-ticks-etc

 

Leave a Reply