Bar Plots with Python

Bar Plots Tutorial with Python using Matplotlib

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.], data)
basic bar plot

For each data value in the list, a vertical bar is displayed. The 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.], data, width=1.)
bar plots

Now the bars no longer have space between them. The matplotlib 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)
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) + 0.00, data[0], color='b', width=0.25) + 0.25, data[1], color='r', width=0.25) + 0.50, data[2], color='g', width=0.25)
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), a, color='b'), b, color='r', bottom=a)
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.

Default image
Aman Kharwal

I am a programmer from India, and I am here to guide you with Data Science, Machine Learning, Python, and C++ for free. I hope you will learn a lot in your journey towards Coding, Machine Learning and Artificial Intelligence with me.

Leave a Reply

Do NOT follow this link or you will be banned from the site!