Bar Charts
A bar chart is used to compare different categories. Each category is represented by a bar, and the size of the bar represents its value.
Bar charts make category comparisons easy to see.
If you want to compare sales between products, students between classes, or revenue between countries, a bar chart is usually a better choice than a line chart.
What Is a Bar Chart?
A bar chart represents categories using rectangular bars. The length or height of each bar represents its value.
Example: Product Sales
Laptop → 500 Phone → 800 Tablet → 300 Monitor → 600
A bar chart allows you to compare these products quickly instead of reading each number separately.
When Should You Use a Bar Chart?
Use a bar chart when you want to compare separate categories.
- Sales by product
- Revenue by country
- Number of students by class
- Orders by category
- Website visitors by page
Simple rule
Different categories
↓
Compare their values
↓
Use a bar chart
Bar Chart vs Line Chart
This distinction is important. Don't choose a chart just because you know its syntax.
Line chart
January February March April May
These values have an order. A line chart is useful for showing how something changes over time.
Bar chart
India USA Germany Japan
These are independent categories. A bar chart is useful for comparing them.
Import Matplotlib
We use Matplotlib's pyplot module to create the chart.
import matplotlib.pyplot as plt
The plt name is simply a shorter way to
access matplotlib.pyplot.
Create Your First Bar Chart
Matplotlib uses plt.bar() to create a
vertical bar chart.
import matplotlib.pyplot as plt
products = [
"Laptop",
"Phone",
"Tablet",
"Monitor"
]
sales = [
500,
800,
300,
600
]
plt.bar(
products,
sales
)
plt.show()
The first argument contains the categories and the second argument contains their values.
plt.bar(
categories,
values
)
Understand How the Bars Work
Matplotlib matches each category with its corresponding value.
products = ["Laptop", "Phone", "Tablet"] sales = [500, 800, 300]
This creates:
Laptop → 500 Phone → 800 Tablet → 300
The value controls the height of each bar.
800 | █
700 | █
600 | █
500 | █ █
400 | █ █
300 | █ █ █
200 | █ █ █
100 | █ █ █
+---------------
Laptop Phone Tablet
Add a Title and Labels
A good chart should clearly explain what the data represents.
import matplotlib.pyplot as plt
products = [
"Laptop",
"Phone",
"Tablet",
"Monitor"
]
sales = [
500,
800,
300,
600
]
plt.bar(
products,
sales
)
plt.title("Product Sales")
plt.xlabel("Product")
plt.ylabel("Sales")
plt.show()
Now the chart tells us:
Title ↓ Product Sales X-axis ↓ Product Y-axis ↓ Sales
Display Values on the Bars
Sometimes you want the exact value to appear above each bar.
Matplotlib can do this using bar_label().
import matplotlib.pyplot as plt
products = [
"Laptop",
"Phone",
"Tablet",
"Monitor"
]
sales = [
500,
800,
300,
600
]
bars = plt.bar(
products,
sales
)
plt.bar_label(bars)
plt.title("Product Sales")
plt.xlabel("Product")
plt.ylabel("Sales")
plt.show()
The important part is:
bars = plt.bar(...) plt.bar_label(bars)
First we store the bars in the bars
variable. Then bar_label() adds the values
to them.
Horizontal Bar Charts
You can also display bars horizontally using
plt.barh().
import matplotlib.pyplot as plt
products = [
"Laptop",
"Phone",
"Tablet",
"Monitor"
]
sales = [
500,
800,
300,
600
]
plt.barh(
products,
sales
)
plt.title("Product Sales")
plt.xlabel("Sales")
plt.ylabel("Product")
plt.show()
The difference is simple:
plt.bar()
↓
Vertical bars
plt.barh()
↓
Horizontal bars
Sorting Bar Chart Data
When comparing categories, sorting the values can make the comparison easier.
products = [
"Laptop",
"Phone",
"Tablet",
"Monitor"
]
sales = [
500,
800,
300,
600
]
The values are:
Phone → 800 Monitor → 600 Laptop → 500 Tablet → 300
A sorted bar chart makes the ranking immediately visible.
Phone █████████ 800 Monitor ██████ 600 Laptop █████ 500 Tablet ███ 300
This is particularly useful for rankings and top-performing categories.
Bar Charts With Pandas
You can combine Pandas and Matplotlib just like you did with line charts.
import pandas as pd
import matplotlib.pyplot as plt
sales = pd.DataFrame({
"Product": [
"Laptop",
"Phone",
"Tablet",
"Monitor"
],
"Sales": [
500,
800,
300,
600
]
})
plt.bar(
sales["Product"],
sales["Sales"]
)
plt.title("Product Sales")
plt.xlabel("Product")
plt.ylabel("Sales")
plt.show()
The responsibilities remain separate:
Pandas ↓ Store / clean / analyze data ↓ Matplotlib ↓ Visualize data
Real-World Example: Sales by Country
Imagine an online business wants to compare sales from different countries.
import matplotlib.pyplot as plt
countries = [
"India",
"USA",
"Germany",
"Japan"
]
sales = [
1200,
2500,
1800,
1400
]
bars = plt.bar(
countries,
sales
)
plt.bar_label(bars)
plt.title("Sales by Country")
plt.xlabel("Country")
plt.ylabel("Sales")
plt.show()
The purpose here is comparison, not showing a continuous trend. Therefore, a bar chart makes more sense than a line chart.
Bar Charts in AI
Bar charts are also useful in AI and Machine Learning. For example, you can compare the accuracy of different models.
import matplotlib.pyplot as plt
models = [
"Logistic Regression",
"Decision Tree",
"Random Forest",
"Neural Network"
]
accuracy = [
82,
85,
91,
94
]
bars = plt.bar(
models,
accuracy
)
plt.bar_label(bars)
plt.title("Model Accuracy")
plt.xlabel("Model")
plt.ylabel("Accuracy (%)")
plt.show()
Now you can immediately compare the performance of the models.
Logistic Regression → 82% Decision Tree → 85% Random Forest → 91% Neural Network → 94%
Complete Bar Chart Example
Here is a complete example combining the important concepts.
import matplotlib.pyplot as plt
models = [
"Logistic Regression",
"Decision Tree",
"Random Forest",
"Neural Network"
]
accuracy = [
82,
85,
91,
94
]
bars = plt.bar(
models,
accuracy
)
plt.bar_label(bars)
plt.title("Model Accuracy")
plt.xlabel("Model")
plt.ylabel("Accuracy (%)")
plt.xticks(rotation=15)
plt.show()
The workflow is:
Create categories
↓
Create values
↓
plt.bar()
↓
Add title
↓
Add axis labels
↓
Add values if needed
↓
Display chart
The Most Important Rule
The key question is not "How do I create a bar chart?" It is "Is a bar chart the correct visualization for this data?"
Good use
Product A → 500 Product B → 800 Product C → 300 Product D → 600
These are independent categories that you want to compare.
Better use of a line chart
January → 500 February → 600 March → 700 April → 850
These values represent an ordered progression over time, so a line chart communicates the trend better.
Bar charts are mainly used for comparison.
Use plt.bar() for vertical bars and
plt.barh() for horizontal bars. The
categories represent the things you want to compare,
while the values determine the size of each bar. Bar
charts are especially useful for rankings, category
comparisons, and comparing Machine Learning model
performance.