Building bar charts using Matplotlib | by Mubarak Ganiyu | Towards Data Science

A bar graph is a data visualization technique that can be used to represent numerical values in a dataset to show how different datapoints vary from each other. It utilizes a bar as a measure of magnitudes. The bigger the bar, the higher the number. For instance, if one is comparing wealth, a bigger bar in a bar graph signifies a lot of money compared to a smaller bar.
The premier league 2019/20 mid-season highest goal scorers dataset was loaded using Jupyter Notebook on IBM Watson Studio.
In order to run codes that will generate a bar graph, a data visualization package called matplotlib is imported. Below is a set of codes for importing the bar graph. Matplotlib is used in python programming language as a plotting library.
## Import data visualization packagesimport matplotlib.pyplot as plt%matplotlib inline
The bar graph is built after installing the package by running the set of codes below.
plt.bar("Player", "Stat", data = df_goal, color = "blue")plt.xlabel("Players")plt.ylabel("Goal Scored")plt.title("Highest goal scorers in the Premier league 2019-20 by mid-season")plt.show()
plt.bar(“Player”, “Stat”, data = df_goal, color = “blue”) is used to signify that a bar graph wants to be plotted using the Player column as the x-axis, the Stat column as the y-axis, the df_goal dataset is to be used and the color of the bars are blue. plt.xlabel(“Players”) and plt.ylabel(“Goal Scored”) are used to label the x-axis and y-axis respectively. plt.title(“Highest goal scorers in the Premier league 2019–20 by mid-season”) is used to make a title for the graph. plt.show( ) is used to generate the graph using the previous commands.
The result of the code ran above can be viewed below.
It can be observed in the bar graph above that the x-axis ticks cannot be seen properly. Thus, the x-axis ticks can become visible after rotation. Below is a set of codes that modifies the bar graph.
plt.bar("Player", "Stat", data = df_goal, color = "blue")plt.xlabel("Players")plt.xticks(rotation = 90)plt.ylabel("Goal Scored")plt.title("Highest goal scorers in the Premier league 2019-20 by mid-season")plt.show()
By adding plt.xticks(rotation = 90), the bar graph can be transformed into:
This is a cleaner bar graph with the x-axis ticks now vertical and visible.
A horizontal bar graph can also be built by changing the plt.bar to plt.barh in the previous set of codes.
plt.barh("Player", "Stat", data = df_goal, color = "red") plt.xlabel("Players") plt.ylabel("Goal Scored") plt.title("Highest goal scorers in the Premier league 2019-20 by mid-season")plt.show()
Both the bar graph and the horizontal bar graphs can be modified to look more beautiful.
The bar graph can be modified by running the set of codes below.
df_goal2 = df_goal[['Player', 'Stat']].sort_values(by = 'Stat', ascending = True)ind = df_goal2.set_index("Player", inplace = True)## A modified bar graphbar = df_goal2.plot(kind='bar',figsize=(30, 16), color = "blue", legend = None)barplt.yticks(fontsize = 24)plt.xticks(ind,fontsize = 18)plt.xlabel("Players", fontsize = 20)plt.ylabel("Goal scored", fontsize = 20)plt.title("Higehst goal scorers in the premier league mid-season of 2019/20", fontsize=32)bar.spines['top'].set_visible(False)bar.spines['right'].set_visible(False)bar.spines['bottom'].set_linewidth(0.5)bar.spines['left'].set_visible(True)plt.show()
A few additions were made compared to the regular codes for building a bar graph. It can be seen above an object called bar is created by using the newly-formed dataset, df_goal2, to design a bar graph with the code, bar = df_goal2.plot(kind=’bar’,figsize=(30, 16), color = “blue”, legend = None). The ind was used to store the index that would be represented by the x-axis.
The spines were removed on the top and right sides but the bottom and left spines were left visible using the codes: bar.spines[‘top’].set_visible(False), bar.spines[‘right’].set_visible(False), bar.spines[‘bottom’].set_linewidth(0.5) and bar.spines[‘left’].set_visible(True).
The result of these codes can be seen below.
The same process for beautifying the bar graph is applied for the horizontal bar graph. Below is a set of codes for building the beautified version.
## A modified horizontal bar graphbarh = df_goal2.plot(kind='barh',figsize=(30, 16), color = "red", legend = None)barhplt.yticks(fontsize = 24)plt.xticks(ind,fontsize = 18)plt.xlabel("Goal scored", fontsize = 22)plt.ylabel("Players", fontsize = 22)plt.title("Higehst goal scorers in the premier league mid-season of 2019/20", fontsize=32)barh.spines['top'].set_visible(False)barh.spines['right'].set_visible(False)barh.spines['bottom'].set_linewidth(0.5)barh.spines['left'].set_visible(True)plt.show()
After running the codes, the result came out as the horizontal bar graph below:
Same observations that was made about the spines in the beautified bar graph can be made about the spines in this graph as well.
Bar graphs are very useful in multiple numerical applications such as showing how people’s health improvements have changed over time, depicting how wealth increases and other interesting concepts.
The full version of the code that was used for this project can be found here.