0
0
Data Analysis Pythondata~5 mins

Histograms in Data Analysis Python

Choose your learning style9 modes available
Introduction

A histogram helps us see how data is spread out by grouping values into bars. It shows which values happen more often.

To understand the distribution of exam scores in a class.
To check how daily temperatures vary over a month.
To see the frequency of different ages in a group of people.
To analyze how many products fall into different price ranges.
Syntax
Data Analysis Python
import matplotlib.pyplot as plt

plt.hist(data, bins=number_of_bins, color='color_name', edgecolor='color_name')
plt.title('Title')
plt.xlabel('X-axis label')
plt.ylabel('Frequency')
plt.show()

data is a list or array of numbers you want to plot.

bins controls how many bars the histogram has.

Examples
This creates a histogram of ages with 5 bars, blue color, and black edges.
Data Analysis Python
import matplotlib.pyplot as plt

ages = [22, 25, 29, 22, 30, 25, 27, 29, 30, 22]
plt.hist(ages, bins=5, color='blue', edgecolor='black')
plt.show()
This shows test scores grouped into 4 ranges with labels and a title.
Data Analysis Python
import matplotlib.pyplot as plt

scores = [80, 85, 90, 75, 70, 95, 85, 80]
plt.hist(scores, bins=4, color='green')
plt.title('Test Scores')
plt.xlabel('Score Range')
plt.ylabel('Number of Students')
plt.show()
Sample Program

This program shows how heights are spread in 5 groups. The bars show how many people fall into each height range.

Data Analysis Python
import matplotlib.pyplot as plt

# Sample data: heights of 15 people in cm
heights = [160, 165, 170, 160, 175, 180, 170, 165, 160, 175, 180, 185, 170, 165, 160]

# Create histogram with 5 bins
plt.hist(heights, bins=5, color='purple', edgecolor='black')

# Add title and labels
plt.title('Height Distribution')
plt.xlabel('Height (cm)')
plt.ylabel('Number of People')

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

If you choose too few bins, the histogram may hide details.

If you choose too many bins, the histogram may look noisy.

Always label your axes and add a title to explain the chart.

Summary

Histograms group data into bars to show frequency.

Use bins to control how detailed the groups are.

They help understand data distribution quickly.