Frequency plot in Python/Pandas DataFrame using Matplotlib

A frequency plot visualizes how often each value appears in a dataset. In Python, you can create frequency plots from Pandas DataFrames using Matplotlib's plotting capabilities.

Basic Frequency Plot

Here's how to create a simple frequency plot using value_counts() and Matplotlib ?

import pandas as pd
import matplotlib.pyplot as plt

# Configure plot settings
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

# Create a DataFrame
df = pd.DataFrame({'numbers': [2, 4, 1, 4, 3, 2, 1, 3, 2, 4]})

# Create frequency plot
fig, ax = plt.subplots()
df['numbers'].value_counts().plot(ax=ax, kind='bar', xlabel='numbers', ylabel='frequency')

plt.show()

Understanding the Process

The frequency plot creation involves these key steps ?

  • Use value_counts() to count occurrences of each unique value
  • Apply plot() with kind='bar' to create a bar chart
  • Specify axis labels using xlabel and ylabel parameters
  • Display the plot with plt.show()

Multiple Column Frequency Plot

You can also create frequency plots for multiple columns ?

import pandas as pd
import matplotlib.pyplot as plt

# Create DataFrame with multiple columns
data = {
    'category': ['A', 'B', 'A', 'C', 'B', 'A', 'C', 'A'],
    'grade': [85, 92, 78, 88, 95, 82, 90, 87]
}
df = pd.DataFrame(data)

# Plot frequency of categories
plt.figure(figsize=(8, 4))
df['category'].value_counts().plot(kind='bar', color='skyblue', edgecolor='black')
plt.title('Frequency of Categories')
plt.xlabel('Category')
plt.ylabel('Count')
plt.xticks(rotation=0)
plt.show()

Customizing Frequency Plots

You can enhance frequency plots with colors, titles, and styling ?

import pandas as pd
import matplotlib.pyplot as plt

# Sample data
scores = [75, 82, 90, 78, 85, 92, 88, 75, 90, 85, 78, 92]
df = pd.DataFrame({'test_scores': scores})

# Create customized frequency plot
plt.figure(figsize=(10, 6))
freq_plot = df['test_scores'].value_counts().sort_index()
freq_plot.plot(kind='bar', color=['red', 'green', 'blue', 'orange', 'purple'], 
               alpha=0.7, edgecolor='black', linewidth=1.2)

plt.title('Test Score Frequency Distribution', fontsize=16, fontweight='bold')
plt.xlabel('Test Scores', fontsize=12)
plt.ylabel('Frequency', fontsize=12)
plt.grid(axis='y', alpha=0.3)
plt.xticks(rotation=45)
plt.show()

Comparison Table

Method Use Case Customization Level
value_counts().plot() Quick frequency plots Basic
plt.hist() Continuous data binning Medium
sns.countplot() Categorical frequency with styling High

Conclusion

Use value_counts().plot(kind='bar') for quick frequency visualization of categorical data. Combine with Matplotlib styling options for professional-looking plots with custom colors, labels, and formatting.

---
Updated on: 2026-03-25T22:43:08+05:30

16K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements