Bar Plots with Python

Bar charts are typically used to visualize the quantities associated with a set of items. In this article, I will introduce you to a data science tutorial on how to plot bar plots with Python.

When to use Bar Charts?

To represent the data accurately you must choose the appropriate graph depending on the nature of the data and the task at hand. Bar plots are suitable for visualizing accounts.

Also, Read – 100+ Machine Learning Projects Solved and Explained.

Bar Plots encode quantities by length, which is a very precise visual encoding and preferred over the angle-based strategy used in pie charts. Often, the accounts we want to represent are sums across multiple categories.

There are several options for visualizing this data using bar charts. Stacked bar charts are the best choice if we are primarily interested in comparing overall quantities between items, but also want to illustrate how each category contributes to totals.

However, if our main goal is to allow comparisons of values ​​between categories within each item while still allowing comparisons between items, a grouped bar chart is the ideal solution.

Bar Plots with Python

In this section, I will take you through how to visualize Bar plots with Python by using the matplotlib library. Let’s start by plotting a basic bar plot:

import pandas as pd
import matplotlib.pyplot as plt
data = [5., 25., 50., 20.]
plt.bar(range(len(data)), data)
plt.show()
basic bar plot

For each data value in the list, a vertical bar is displayed. The pyplot.bar() function takes two arguments; the x coordinate for each bar and the height of each bar. Here we are using coordinates 0, 1, 2, and so on, for each bar, which is the purpose of range(len(data)).

Let’s see how we can increase the thickness of the bars:

data = [5., 25., 50., 20.]
plt.bar(range(len(data)), data, width=1.)
plt.show()
bar plots

Now the bars no longer have space between them. The matplotlib pyplot.bar() function will not handle the bar placement and thickness. The programmer is in charge. This flexibility allows you to create many variations on bar charts.

Horizontal Bar Plots with Python

Now let’s see how to visualize the horizontal bar charts with Python:

import pandas as pd
import matplotlib.pyplot as plt
data = [5., 25., 50., 20.]
plt.barh(range(len(data)), data)
plt.show()
horizontal bar chart

That’s it, you just need to use barh(), instead of bar().

Multiple Bar Plots with Python

When comparing multiple quantities and when changing a variable, we might want a bar chart where we have bars of one colour for a quantity value. We can draw several histograms by playing with the thickness and the positions of the bars as follows:

import numpy as np
data = [[5., 25., 50., 20.],
        [4., 23., 51., 17.],
        [6., 22., 52., 19.]]
x = np.arange(4)
plt.bar(x + 0.00, data[0], color='b', width=0.25)
plt.bar(x + 0.25, data[1], color='r', width=0.25)
plt.bar(x + 0.50, data[2], color='g', width=0.25)
plt.show()
multiple bar charts

Stacked bar charts are also possible in the matplotlib library in Python. Let’s see how to visualize a stacked bar chart with Python:

a = [5., 30., 45., 22.]
b = [5., 25., 50., 20.]
x = range(4)
plt.bar(x, a, color='b')
plt.bar(x, b, color='r', bottom=a)
plt.show()
stacked bar plots

I hope you liked this article on how to visualize bar plots with Python by using the matplotlib library. Feel free to ask your valuable questions in the comments section below.

Aman Kharwal
Aman Kharwal

I'm a writer and data scientist on a mission to educate others about the incredible power of data📈.

Articles: 1433

Leave a Reply