Plotting Bar Charts in Python (Tutorial)

Plotting Bar Charts In Python

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 matplotlib module.

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 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 = [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, y, tick_label = labels, width = 0.5, color = colors)

#X-AXIS Label

#Y-AXIS Label

#Bar Chart Title
p.title("Weekly Sales Graph")

#Displaying the graph


Python Plotting Bar Chart Example

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.

The xlabel() and 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
Python 25 29 41 48 59
Java 99 73 67 59 54
JavaScript 44 43 49 45 45

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.


import matplotlib.pyplot as p

X1 = [1, 2, 3, 4, 5]
X2 = [1.25, 2.25, 3.25, 4.25, 5.25]
X3 = [1.5, 2.5, 3.5, 4.5, 5.5]

Y1 = [25, 29, 41, 48, 59]
Y2 = [99, 73, 67, 59, 54]
Y3 = [44, 43, 49, 45, 45]

labels = ["2015", "2016", "2017", "2018", "2019"], Y1, color = '#306998', width = 0.25, tick_label = labels), Y2, color = '#F89820', width = 0.25, tick_label = labels), Y3, color = '#F0DB4F', width = 0.25, tick_label = labels)

#X-AXIS Label

#Y-AXIS Label

#Bar Chart Title
p.title("POPULARITY OF LANGUAGES (2015-2019)")

#To Show Legend
p.legend(labels=["Python", "Java", "JavaScript"])

#Displaying the graph


Python Complex Bar Chart Example

As you can observe clearly, the bar chart depicts that from 2015 to 2019, there’s a rising trend of Python, a falling trend of Java and the JavaScript’s trend remains almost the same.

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, Y1, width, tick_label = labels, color='#306998'), Y2, width, tick_label = labels, bottom=Y1, color="#F89820")

#X-AXIS Label

#Y-AXIS Label
p.title('POPULARITY - PYTHON Vs JAVA (2015-2019)')

#To Show Legend
p.legend(labels=["Python", "Java"])

#To Show Bar Chart


Python Matplotlib Complex Bar Chart Example

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!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

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