Matplotlib Bar Chart Tutorial in Python

This is the full tutorial on Matplotlib bar chart in Python.

What are Bar graphs..?

Let us take an example of Icecream flavors to know the number of people who like the different flavors. Now, we represent this data by drawing two perpendicular lines as shown in the below figure.

Bar Chart Using Matplotlib in Python

Such graphs are called Bar Chart and these help us to analyze the data.  Just by looking at the graph we can say most of the people prefer the chocolate flavor. Generally, we draw the graphs manually on the graph paper. But when it comes to showing the graph digitally we need to do the proper programming by using the functions and libraries.

Bar Chart Using Matplotlib in Python

In this tutorial, we are going to represent the bar chart using the matplotlib library.

The bar chart is a way of visualizing the data in which we have some discrete values.

Let us take an example of the year-wise percentage of an engineering student of cse stream.

import matplotlib.pyplot as plt
Percentage={"1st Year":80 ,"2nd Year":78 ,"3rd Year":89, "4th Year":90}
ticks=range(1,5)
height=list(Percentage.values())
tick_label=list(Percentage.keys())
plt.bar(ticks,height)
plt.show()

First, we need to import the matplotlib library and referring to it as plt. We created a dictionary as a percentage that holds the keys and their values as a year and the percentage.

Next, we need to take the x-axis values on which we plot the bar chart. And we also specified the heights of the bar of the list and dictionary values.
Finally, we specified the labels for our ticks that are keys as 1st year, 2nd year, 3rd, 4th year. After specifying all the data we need to use the bar() function in which we pass the ticks and heights of the bar as shown in the above code. And the show() function is called in order to see the graph

Output:

matplotlib library

From the output, we can see that the x-axis contains numeric values in place of a label. So we need to specify the following argument in the bar().

plt.bar(ticks,height,tick_label=tick_label)

Output:

plt.bar() in matplotlib

Now we can see the labels as 1st year, 2nd year, 3rd year, 4th year.

Also, we can make a more effective bar graph by change the color of the bar and width bar. And also can give the title, x-axis label, y-axis label to bar chart in the bar() function.

plt.bar(ticks,height,tick_label=tick_label,color=['b','m'],width=0.5)
plt.title("Year Wise Percentage")
plt.ylabel("PERCENTAGE")
plt.xlabel("B.E YEARS")

Now we can observe the complete structured bar chart.

Output:

bar() function in matplotlib

  • Next, let us see how to draw a horizontal bar chart by using the barh() function.
    In horizontal we pass height but not width in the barh().

Let us consider the example.

import matplotlib.pyplot as plt
Percentage={"1st Year":79 ,"2nd Year":78 ,"3rd Year":89, "4th Year":90}
ticks=range(1,5)
height=list(Percentage.values())
tick_label=list(Percentage.keys())
plt.barh(ticks,height,tick_label=tick_label,color=['b','m'],height=0.5)
plt.title("Year Wise Percentage")
plt.ylabel("PERCENTAGE")
plt.xlabel("B.E YEARS")
plt.show()

Output:

plt.barh in Python

  • We also have a comparison bar chart.

Let us consider an example of comparing the percentage of Yours and your friend. As we already did first-person percentage.
Now let us take another dictionary and plot them side by side. Consider the example.

import matplotlib.pyplot as plt
import numpy as np
percentage2={"1st Year":62 ,"2nd Year":80 ,"3rd Year":89, "4th Year":80}
index = np.arange(4)
height2=list(percentage2.values())
bar_width=0.35
plt.bar(index, height, label="tom", width=bar_width)
plt.bar(index+bar_width, height2, label="sam", width=bar_width)
plt.xticks(index+bar_width/2, tick_label)
plt.title("COMPARISION OF BOTH FRIENDS YEAR WISE PERCENTAGES OF CSE ",fontsize=15)
plt.legend()
plt.show()

Here, instead of range function, we use the numpy array because the index that is specified is going to be used for the percentage of first-person. So, for the percentage for the second person, we need different ticks. The bar will be different but on the same plot. So, this can be done by simply adding the bar width.
And we have taken the two bar functions for both person percentage along with their label names.
The legend function used to make a nice looking legend for both the bar charts. So, we can identify them easily.

Output:

legend for both the bar charts

By looking at the bar graph we can identify that the blue color bar is about tom data and orange color is about sam data.
Visualization will clear and neat.

Also read:

Leave a Reply

Your email address will not be published. Required fields are marked *