Python tutorial on Matplotlib bar charts, covering basic and advanced bar charts with practical examples.
last modified February 25, 2025
Matplotlib is a powerful Python library for creating static, animated, and interactive visualizations. Bar charts are one of the most common types of charts used to compare categorical data. This tutorial covers how to create various types of bar charts using Matplotlib.
Bar charts are ideal for visualizing discrete data, such as counts or percentages across categories. Matplotlib provides a flexible and easy-to-use interface for creating bar charts with customizations.
This example demonstrates how to create a basic bar chart.
basic_bar_chart.py
import matplotlib.pyplot as plt
categories = [‘A’, ‘B’, ‘C’, ‘D’] values = [10, 20, 15, 25]
plt.bar(categories, values)
plt.xlabel(“Categories”) plt.ylabel(“Values”) plt.title(“Basic Bar Chart”)
plt.show()
The plt.bar() function is used to create a bar chart. The plt.show() function displays the chart.
This example shows how to create a horizontal bar chart.
horizontal_bar_chart.py
import matplotlib.pyplot as plt
categories = [‘A’, ‘B’, ‘C’, ‘D’] values = [10, 20, 15, 25]
plt.barh(categories, values)
plt.xlabel(“Values”) plt.ylabel(“Categories”) plt.title(“Horizontal Bar Chart”)
plt.show()
The plt.barh() function is used to create a horizontal bar chart.
This example demonstrates how to create a grouped bar chart.
grouped_bar_chart.py
import matplotlib.pyplot as plt import numpy as np
categories = [‘A’, ‘B’, ‘C’, ‘D’] values1 = [10, 20, 15, 25] values2 = [15, 25, 20, 30]
bar_width = 0.35
x = np.arange(len(categories))
plt.bar(x - bar_width/2, values1, width=bar_width, label=“Group 1”) plt.bar(x + bar_width/2, values2, width=bar_width, label=“Group 2”)
plt.xlabel(“Categories”) plt.ylabel(“Values”) plt.title(“Grouped Bar Chart”) plt.xticks(x, categories) plt.legend()
plt.show()
The np.arange() function is used to create positions for the bars. The width parameter controls the width of the bars.
This example shows how to create a stacked bar chart.
stacked_bar_chart.py
import matplotlib.pyplot as plt
categories = [‘A’, ‘B’, ‘C’, ‘D’] values1 = [10, 20, 15, 25] values2 = [15, 25, 20, 30]
plt.bar(categories, values1, label=“Group 1”) plt.bar(categories, values2, bottom=values1, label=“Group 2”)
plt.xlabel(“Categories”) plt.ylabel(“Values”) plt.title(“Stacked Bar Chart”) plt.legend()
plt.show()
The bottom parameter is used to stack the second group of bars on top of the first group.
This example demonstrates how to customize bar charts with colors, edge colors, and patterns.
custom_bar_chart.py
import matplotlib.pyplot as plt
categories = [‘A’, ‘B’, ‘C’, ‘D’] values = [10, 20, 15, 25]
plt.bar(categories, values, color=“skyblue”, edgecolor=“black”, hatch="/")
plt.xlabel(“Categories”) plt.ylabel(“Values”) plt.title(“Custom Bar Chart”)
plt.show()
The color, edgecolor, and hatch parameters are used to customize the appearance of the bars.
Label Axes Clearly: Always label the X and Y axes to make the chart understandable.
Use Legends: Add legends when plotting multiple groups to differentiate them.
Choose Appropriate Colors: Use contrasting colors for multiple groups to improve readability.
Limit Categories: Avoid cluttering the chart with too many categories.
Matplotlib Bar Chart Documentation
In this article, we have explored various types of bar charts using Matplotlib, including basic, horizontal, grouped, stacked, and custom bar charts.
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.
List all Python tutorials.