Creating Bar and Pie Charts
Not all data is best shown with lines.
If you're comparing categories or showing proportions, bar charts
and pie charts
are better options.
In this lesson, you'll learn how to create both types of charts.
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 show parts of a whole.
For example, market share percentages or budget distribution.
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 are best when you want to highlight relative proportions, not exact quantities.
Want to learn more?
Join CodeFriends Plus membership or enroll in a course to start your journey.