This is a detailed tutorial on Plotting Bar Charts in Python. Learn to draw different kinds of bar graphs with a lot of options using the
Plotting Bar Charts in Python
In our last tutorial, we learned to create single line and multi-line graphs using the
matplotlib module in Python. If you have not checked that tutorial yet, I highly recommend you to go through it first before beginning further with this one.
If you do not have the
matplotlib module installed yet, you can quickly install it using the following command.
pip install matplotlib
To draw a bar chart with this module, all you need to do is to import this module in your Python program and then have to use its
pyplot.bar() method. You just have to provide some arguments to this method and you’re all set to see a bar chart drawn as per your provided inputs.
A Simple Bar Chart
Have a look at the following code and the bar chart shown in the output.
import matplotlib.pyplot as p #X-Values x = [1, 2, 3, 4, 5, 6] #Y-Values or Heights of Bars y = [5, 10, 7, 15, 13, 25] #Bar Labels labels = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"] #Bar Colors colors = ["red", "green", "blue"] #Plotting the Bar Chart p.bar(x, y, tick_label = labels, width = 0.5, color = colors) #X-AXIS Label p.xlabel("Days") #Y-AXIS Label p.ylabel("Sales") #Bar Chart Title p.title("Weekly Sales Graph") #Displaying the graph p.show()
Let’s quickly talk about the method
bar() and its arguments according to which the graph is constructed. We have passed 5 different arguments to this method that are described below.
- x. This is a Python List containing the X-Values of the Bar Chart.
- y. This is also a Python List that contains the Y-Values or you can say the heights of the Bars.
- tick_label. This is again a Python List containing the labels for each of the consecutive bars.
- width. It is a numerical argument that defines the width of the bars.
- color. You can pass any number of colors in a Python List here.
ylabel() methods are used to give label to the X-AXIS and Y-AXIS respectively. The method
title() gives a title for the Bar Chart and at the end, we’ve used the show() method to finally display the constructed Bar Chart.
Complex Bar Chart
Although bar charts are commonly used to visualize simple data as also shown in the above example, but you can show much more complex data visualizations with it.
Let’s take the data set of the popularity of three programming languages over the past 5 years. We have the following data set and we want to make bar chart for it. (I got the following data from Google Trends)
|Language / Year||2015||2016||2017||2018||2019|
Note. The numbers are just to have a comparison of each of these languages popularity over the time period of 5 years. Therefore, there is no specific unit associated with them.
Let’s create another complex bar graph using the same data that we have used above but now with a different visualization.
Have a look at the following code.
import matplotlib.pyplot as p X = [0, 1, 2, 3, 4] Y1 = [25, 29, 41, 48, 59] Y2 = [99, 73, 67, 59, 54] labels = ["2015", "2016", "2017", "2018", "2019"] width = 0.5 p.bar(X, Y1, width, tick_label = labels, color='#306998') p.bar(X, Y2, width, tick_label = labels, bottom=Y1, color="#F89820") #X-AXIS Label p.xlabel("YEAR") #Y-AXIS Label p.ylabel("POPULARITY") p.title('POPULARITY - PYTHON Vs JAVA (2015-2019)') #To Show Legend p.legend(labels=["Python", "Java"]) #To Show Bar Chart p.show()
I hope you found this guide useful. If so, do share it with others who are willing to learn Python. If you have any questions related to this article, feel free to ask us in the comments section.
And do not forget to subscribe to WTMatter!