Fundamentals 5 min read

Creating Common Charts with Python Matplotlib: Bar, Pie, Donut, Stacked Bar, and Pareto Charts

This tutorial demonstrates how to use Python's Matplotlib library to create five essential data‑visualization charts—simple bar, pie, donut, stacked bar, and Pareto—by installing the library, explaining each chart’s purpose, and providing ready‑to‑run code examples.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Creating Common Charts with Python Matplotlib: Bar, Pie, Donut, Stacked Bar, and Pareto Charts

In data analysis, data visualization is a crucial skill that helps quickly understand distributions and trends, enabling informed decisions; Python offers powerful libraries such as Matplotlib and Seaborn to make visualization both simple and attractive.

First, ensure the matplotlib library is installed; if not, run pip install matplotlib.

1. Simple Bar Chart – used to compare numeric differences across categories.

import matplotlib.pyplot as plt
# Data
labels = ['A', 'B', 'C', 'D']
values = [10, 25, 30, 20]
# Create bar chart
plt.bar(labels, values)
plt.xlabel('Categories')
plt.ylabel('Values')
plt.title('Simple Bar Chart')
plt.show()

2. Pie Chart – shows each part's proportion of the whole.

from matplotlib import pyplot as plt
labels = ['Apple', 'Banana', 'Cherry', 'Date']
sizes = [20, 30, 25, 25]
plt.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=140)
plt.axis('equal')  # Equal aspect ratio ensures that pie is drawn as a circle.
plt.title('Fruit Distribution')
plt.show()

3. Donut Chart – a variant of the pie chart with a hollow center, useful for comparing multiple proportions.

from matplotlib import pyplot as plt
labels = ['Group A', 'Group B', 'Group C']
sizes = [15, 35, 50]
plt.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=140, wedgeprops=dict(width=0.3))
plt.title('Group Distribution')
plt.show()

4. Stacked Bar Chart – displays sub‑category contributions within each main category.

from matplotlib import pyplot as plt
labels = ['Q1', 'Q2', 'Q3', 'Q4']
men_values = [20, 35, 30, 35]
women_values = [25, 32, 34, 20]
barWidth = 0.35
r1 = range(len(men_values))
r2 = [x + barWidth for x in r1]
plt.bar(r1, men_values, color='b', width=barWidth, label='Men')
plt.bar(r2, women_values, color='r', width=barWidth, label='Women')
plt.xlabel('Quarters')
plt.ylabel('Scores')
plt.title('Scores by quarter')
plt.xticks([r + barWidth/2 for r in range(len(men_values))], labels)
plt.legend()
plt.show()

5. Pareto Chart – combines a bar chart and a line chart to identify the most impactful factors.

import numpy as np
from matplotlib import pyplot as plt
categories = ['A', 'B', 'C', 'D', 'E']
values = [15, 22, 35, 20, 8]
sorted_values = sorted(values, reverse=True)
cumulative_percentage = np.cumsum(sorted_values) / sum(sorted_values) * 100
plt.bar(categories, sorted_values)
plt.plot(cumulative_percentage, marker='o', linestyle='--', color='r')
plt.xlabel('Categories')
plt.ylabel('Values')
plt.title('Pareto Chart')
plt.legend(['Cumulative %'])
plt.show()

In summary, the guide shows how to create bar, pie, donut, stacked bar, and Pareto charts with Matplotlib, highlighting each chart’s unique use case and encouraging the selection of appropriate visualizations to make data stories more compelling.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonTutorialData visualizationcharts
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.