0
0
Data Analysis Pythondata~5 mins

Bar charts in Data Analysis Python

Choose your learning style9 modes available
Introduction

Bar charts help us see and compare numbers easily using bars. They make data simple to understand at a glance.

Comparing sales of different products in a store.
Showing the number of students in different classes.
Visualizing survey results with categories like favorite colors.
Comparing monthly expenses in a budget.
Displaying counts of different types of animals in a zoo.
Syntax
Data Analysis Python
import matplotlib.pyplot as plt

plt.bar(x, height, color=None, width=0.8)
plt.show()

x is a list of categories or labels.

height is a list of numbers for each category.

Examples
Basic bar chart with three fruit categories and their counts.
Data Analysis Python
import matplotlib.pyplot as plt

categories = ['Apples', 'Bananas', 'Cherries']
values = [10, 15, 7]
plt.bar(categories, values)
plt.show()
Bar chart with custom bar color.
Data Analysis Python
import matplotlib.pyplot as plt

categories = ['Red', 'Blue', 'Green']
values = [5, 8, 6]
plt.bar(categories, values, color='skyblue')
plt.show()
Bar chart with thinner bars using the width parameter.
Data Analysis Python
import matplotlib.pyplot as plt

categories = ['A', 'B', 'C']
values = [3, 7, 4]
plt.bar(categories, values, width=0.5)
plt.show()
Sample Program

This program draws a bar chart showing counts of different fruits with orange bars. It also adds a title and labels for clarity.

Data Analysis Python
import matplotlib.pyplot as plt

# Categories and their values
fruits = ['Apple', 'Banana', 'Cherry', 'Date']
counts = [12, 19, 7, 5]

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

# Add title and labels
plt.title('Number of Fruits')
plt.xlabel('Fruit')
plt.ylabel('Count')

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

Bar charts work best for comparing a few categories, not too many.

Always label your axes and add a title to make charts clear.

You can change bar colors and width to improve the look.

Summary

Bar charts show data as bars for easy comparison.

Use plt.bar() with categories and values.

Customize colors, width, and labels for clarity.