0
0
Pandasdata~5 mins

Histogram plots in Pandas

Choose your learning style9 modes available
Introduction

A histogram helps us see how data is spread out by showing how many values fall into different groups or ranges.

To understand the distribution of ages in a group of people.
To check how test scores are spread among students.
To see the frequency of different sales amounts in a store.
To find out if data is mostly low, medium, or high values.
To compare how two sets of data differ in their spread.
Syntax
Pandas
DataFrame['column_name'].hist(bins=number_of_bins, figsize=(width, height))

bins controls how many groups the data is split into.

figsize sets the size of the plot in inches (width, height).

Examples
Basic histogram of the 'age' column with default settings.
Pandas
df['age'].hist()
Histogram of 'score' with 5 groups to see data in fewer ranges.
Pandas
df['score'].hist(bins=5)
Histogram of 'height' with 10 groups and a wider plot size.
Pandas
df['height'].hist(bins=10, figsize=(8,4))
Sample Program

This code creates a small dataset of ages and shows a histogram with 5 groups. It also adds a title and labels to the plot for clarity.

Pandas
import pandas as pd
import matplotlib.pyplot as plt

# Create sample data
 data = {'age': [22, 25, 29, 24, 30, 22, 23, 27, 28, 24, 26, 25, 29, 30, 22]}
 df = pd.DataFrame(data)

# Plot histogram of 'age' with 5 bins
 df['age'].hist(bins=5, figsize=(6,4))
 plt.title('Age Distribution')
 plt.xlabel('Age')
 plt.ylabel('Frequency')
 plt.show()
OutputSuccess
Important Notes

Histograms work best with numeric data.

You can adjust bins to see more or less detail in the data spread.

Use plt.show() to display the plot when running code outside notebooks.

Summary

Histograms show how data values are spread across ranges.

Use bins to control the number of groups in the histogram.

Adding titles and labels helps make the plot easier to understand.