0
0
Matplotlibdata~5 mins

Why bar charts compare categories in Matplotlib

Choose your learning style9 modes available
Introduction

Bar charts help us see differences between groups easily. They show how big or small each category is compared to others.

Comparing sales numbers for different products in a store.
Showing the number of students in different classes.
Comparing votes received by candidates in an election.
Displaying the count of different types of fruits sold.
Syntax
Matplotlib
import matplotlib.pyplot as plt

categories = ['A', 'B', 'C']
values = [10, 20, 15]

plt.bar(categories, values)
plt.show()

plt.bar() creates vertical bars for each category.

The first argument is a list of categories (labels).

The second argument is the list of values for each category.

Examples
Simple bar chart comparing fruit counts.
Matplotlib
import matplotlib.pyplot as plt

categories = ['Apples', 'Bananas', 'Cherries']
values = [5, 7, 3]

plt.bar(categories, values)
plt.show()
Bar chart with green bars to compare pet counts.
Matplotlib
import matplotlib.pyplot as plt

categories = ['Dogs', 'Cats', 'Birds']
values = [12, 15, 7]

plt.bar(categories, values, color='green')
plt.show()
Sample Program

This program shows a bar chart comparing how many fruits were sold. Each bar's height shows the number sold for that fruit.

Matplotlib
import matplotlib.pyplot as plt

# Categories to compare
fruits = ['Oranges', 'Apples', 'Bananas', 'Grapes']

# Values for each category
sales = [25, 40, 30, 10]

# Create bar chart
plt.bar(fruits, sales, color='orange')

# Add title and labels
plt.title('Fruit Sales Comparison')
plt.xlabel('Fruit')
plt.ylabel('Number Sold')

# Show the chart
plt.show()
OutputSuccess
Important Notes

Bar charts are best for comparing a few categories clearly.

Use different colors or labels to make charts easier to read.

Summary

Bar charts show differences between categories by using bars of different heights.

They help us quickly compare numbers for groups like products or votes.