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

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

That is why in this article we will show you 15 examples that you can plot a bar chart using python.

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.

Using Matplotlib To Plot A Bar Chart

Matplotlib is by far one of the most popular python data visualizations packages currently in existence. They offer many visualization charts, one of them being is their bar charts. Matplotlib offers users to graph bar charts through its “bar()” function.

1. Basic bar chart

To plot a basic bar chart using matplotlib, we just need to declare our x and y values. In our current bar chart, I declared the programming language(language) as our x-axis and students as our y-axis.

basic bar chart matplotlib
basic bar chart matplotlib code

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()” and declare our x and y-axis, much like what we did with our normal bar chart previously.

Horizontal bar chart
Horizontal bar chart python code matplotlib

3. Colored bar charts

An option that matplotlib offers in their “plt.bar()” function is the ability to change the color of the bars. This can be completed through the use of “color” attribute within the “plt.bar()” function.

colored bar chart
colored bar chart python code

Similarly, individual bar colors can also be changed, this can be used 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 order listed in the x-axis.

bar chart with adjusted singular bar color

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 “plt.bar()” function.

bar chart with outline

4. Bar chart with fill pattern

bar chart with fill
bar chart python code with fill

5. Bar chart with error bars

An error bar is a line on 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 “plt.bar()’ function using the attribute “yerr”.

error bar chart
error bar chart python code

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 “plt.bar()” 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 “px.bar()”.

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 “px.bar” 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