How To Plot A Bar Chart Using Python (15 Examples)

Bar charts are a great way to compare data, it’s by far one of the most popular graphs to represent nominal and ordinal categories, and by combining this with the use of python programming, we can definitely produce some awesome bar charts.

The simplest way to plot a bar chart is by using from matplotlib. However, as you may know, there are several ways to plot bar charts, finding codes and tutorials to plot each of these bar charts can be quite tedious.

That is why in this article we combined all the different ways that you can plot a bar chart using matplotlib, plotly and seaborn.

Free Bonus: Click Here To Get A FREE Introduction to Data Science in Python and learn how to effectively analyze and visualize your data in Python 3 by leveraging popular Data Science modules like Matplotlib (for charts and graphs) and pandas (for tabular data)

Using Matplotlib To Plot A Bar Chart

Matplotlib is one of the most popular python data visualization packages currently in existence. They offer many visualization charts, one of them being their bar charts.

You can graph a bar chart in Matplotlib using the function.

Below, we’ll show you a few different bar chart graphs that you can do with matplotlib package.

Before you can start building your bar charts, you’ll need to install matplotlib and import it to your development platform. You can do this using the code below.

!pip install matplotlib
import matplotlib.pyplot as plt

1. Basic bar chart

To plot a basic bar chart using matplotlib, we just need to declare our x and y values and input these x and y values into the function.

basic bar chart matplotlib
language = ['C', 'C++', 'Java', 'Python', 'PHP']
students = [23,11,32,22,11], students)

Lines 1 – 2: This part is optional, we just created two lists to create our data.
Line 4: using our imported “import matplotlib.pyplot as plt” we used the bar function with the x axis being our language list and y-axis being the number of students
Line 6: just outputs the graph

2. Horizontal bar chart

A horizontal bar chart is just like a normal bar chart, except our categories are displayed on the y-axis rather than the x-axis.

To make a horizontal bar chart in matplotlib we can use the function plt.barh() (extra h stands for horizontal in plt.barh())and declare our x and y-axis, much like what we did with our normal bar chart previously.

Horizontal bar chart in matplotlib

3. Colored bar charts

An option that matplotlib offers in their function is the ability to change the color of the bars.

This can be completed by declaring the color attribute within the function and then assigning a color to the attribute.

colored bar chart,students, color = 'green')

Similarly, individual bar colors can also be changed, which is pretty useful to represent key statistics, for instance, coloring the maximum value red.

To color individual bars we can declare a new list and input the corresponding bar colors as per their corresponding order listed in the x-axis.

bar chart with adjusted singular bar color
language = ['C', 'C++', 'Java', 'Python', 'PHP']
students = [23,11,32,22,11]
colors = ['grey', 'grey', 'red', 'grey', 'grey'], students, color = colors)

Another option that matplotlib offers is the ability to change the outline of our bar charts. This can be declared by using the attribute edgecolor within our function.

bar chart with outline,students, color = 'lightgrey', edgecolor= 'blue')

4. Bar chart with fill pattern

Another neat little thing you can do with your bar charts is by adding patterns, although this is not widely used in graphing standards (due to its lack of aesthetic appeal).

You can add some patterns in the hatch attribute of your function.

bar chart with fill,students, hatc = '/')

5. Bar chart with error bars

An error bar is a line within our individual bars that represents the uncertainty or variation of the data. We use error bars to indicate the spread of our data.

To input error bars on our individual bar chart, we need to declare a new variable that lists the variance of our individual bars, then declare the values into our function using the attribute yerr.

error bar chart
language = ['C', 'C++', 'Java', 'Python', 'PHP']
students = [23,11,32,22,11]
variance = [5,6,7,4,3],students, yerr=variance)

6. Stacked Bar chart

A stacked bar chart is an extension of a standard bar chart, where instead of one categorical variable included within a single bar, it can be two or even more.

To create a stacked bar chart in python we need to declare our function “” to the number of times our category appears, in my case, it was declared twice, one for the men category the other for women. Matplotlib understands this automatically and creates a stacked bar chart.

stacked bar graph

7. Stacked Bar chart with text annotations

To annotate our bar charts we can use the function “plt.annotate()” this function allows us to place text and also indicators such as arrows within our graph. Basically, we need to declare the x and y-axis for the text and arrow to be positioned.

stacked bar chart with text annotations
Python code for annotated text bar chart in matplotlib

8. Stacked bar chart with error bars

Similarly, like our standard bar chart we can also include multiple variances within our stacked bar chart of each male and female category. To input our variance, we can declare the variance values within the respective bar plots using the attribute “yerr”.

Stacked bar chart with error bars using matplotlib
stacked bar chart with error bars python code

9. Grouped bar chart

Another way to visualize our multi-category values is through the use of a grouped bar chart. A grouped bar chart is also known as a multi-series bar chart or clustered bar chart, it essentially visualizes data in a grouped formation, where each category has multiple bars to represent its secondary category.

To create a grouped bar chart the only addition is we need to declare an “np.arange()” function for our x-axis, next we need to declare the x-axis within the subplots of our bar function and divide the width for left and right for men and women.

grouped bar graph
grouped bar graph python code

10. Grouped bar chart with value annotations

Value annotations list the values of each individual bar, to do this via matplotlib we can call the function “ax.bar_label()” and input the bar plot variable within our function.

grouped bar chart with value annotations
python code for grouped bar graph

Using Plotly To Plot A Bar Chart

Another python data visualization package we can use is plotly. The unique benefit that plotly offers compared to matplotlib is the ability to provide better visualization tools and a more interactive chart. To make a bar chart using plotly we can use the function “”.

11. Heatmap Bar Chart

Unlike the traditional bar chart, a heatmap bar chart provides an additional layer of visualization where it changes the color of each individual bar based on a “temperature scale”.

In the graph below it visualizes student enrolment throughout the years where the color represents student debt.

To create the additional temperature scale we can use plotly’s “” additional attribute “color”, and assign the extra numerical category to the color attribute.

Heated bar chart with plotly
heated bar graph plotly using python

12. Faceted Bar Chart

A faceted bar chart assists in the comparison of information that has multiple variables. It is essentially a giant graph that consists of multiple subplots which have the same set of axes.

Plotly allows you to make a faceted bar chart with the attribute “fact_row” for rows and “facet_col” for columns.

In our case the attribute “fact_row” was only used, this row attribute visualizes two things, one where it shows a bar chart of people who do not smoke and the other individuals who do smoke.

multi row faceted bar chart python
code for faceted bar chart

13. Bullet bar chart

A bullet chart is a variation of a bar chart, it is widely used as a performance indicator. The difference is it provides additional indicators such as feature measure, comparative range, and comparative measure, which are basically key performance indicators.

To create our bullet bar chart, we need to import our package plotly.graph_objects. And use the function “go.figure(go.Indicator()” and declare the mode as “number+guage+delta” this is where the bullet chart is stored.

Afterward, the bullet chart can be adjusted. To get a better idea, it is better to review the code below.

bullet bar chart using plotly
bullet bar graph python code

Using seaborn to plot a bar chart

Another visualization package we can use is seaborn which is a data visualization library built on top of matplotlib the added benefit is it provides greater visualization tools and customized themes that matplotlib lacks.

14. Bar chart with themes

You can adjust the general styling and the background of your charts through the use of seaborn’s “sns.set_style” some styles you can choose from are darkgrid, whitegrid, dark, white, and ticks.

seaborn bar graph with theme

15. Bar chart with error bars

Much like matplotlib, seaborn allows the ability to create error bars, however, the difference is seaborn automatically creates error bars with confidence intervals of 95%. To turn off this function, you can set the attribute ci=none within the “sns.barplot()” function.

matplotlib error bars with adjusted size

The good thing about seaborn is it provides the ability to change our error bar styles. Some adjustments that can be changed are the confidence interval(ci) and the capsizes(capsize).

error bar graph with capsize in seaborn

When Should You Use A Bar chart?

You should plan to use a bar chart when it is required to display a distribution of data points or to compare metric values across different subgroups of your data. A bar chart allows us to see which groups have the most numerous amounts of data, as well as how other groups compare to one another. Therefore bar charts provide us the ability to see the distribution of data across each category.