Skip to main content
Practice

Creating Bar and Pie Charts

Not all datasets are best visualized with lines.

When you need to compare categories or show proportions, bar charts and pie charts are often more effective.

In this lesson, you’ll learn how to create both using Matplotlib.


Bar Charts

Use plt.bar() to compare values across categories (e.g. sales per product or number of users per region).

Basic Bar Chart
categories = ["A", "B", "C"]
values = [10, 25, 15]

plt.bar(categories, values)
plt.title("Category Comparison")
plt.xlabel("Category")
plt.ylabel("Value")
plt.show()

Bar charts can be vertical (default) or horizontal using plt.barh().


Pie Charts

Use plt.pie() to visualize parts of a whole, such as market shares, budget allocations, or time spent on activities.

Basic Pie Chart
labels = ["Rent", "Food", "Transport"]
sizes = [40, 35, 25]

plt.pie(sizes, labels=labels)
plt.title("Monthly Expenses Breakdown")
plt.show()

Pie charts work best for showing relative proportions rather than precise numerical comparisons.

Want to learn more?

Join CodeFriends Plus membership or enroll in a course to start your journey.