ApX logoApX logo
ModelsCalculatorHardwareCoursesPricing
ApX logoApX logo
ModelsCalculatorHardwareCoursesPricing

All Courses

Data Visualization with Matplotlib and Seaborn
Chapter 1: Introduction to Data Visualization
What is Data Visualization?
Why Visualize Data in AI and Engineering?
Introduction to Matplotlib
Introduction to Seaborn
Relationship between Matplotlib and Seaborn
Setting Up Your Python Environment
Importing Libraries
Your First Simple Plot
Quiz for Chapter 1
Chapter 2: Fundamentals of Matplotlib Plotting
Anatomy of a Matplotlib Plot: Figure and Axes
Creating a Basic Plotting Script
Line Plots: Plotting Trends
Scatter Plots: Showing Relationships
Adding Titles and Labels
Changing Colors and Line Styles
Setting Axis Limits
Practice: Creating Simple Line and Scatter Plots
Quiz for Chapter 2
Chapter 3: Basic Matplotlib Plot Types
Bar Charts for Comparisons
Creating Vertical and Horizontal Bar Charts
Histograms for Distributions
Understanding Bins in Histograms
Pie Charts for Proportions
Stacking Plots
Hands-on Practical: Plotting Different Data Types
Quiz for Chapter 3
Chapter 4: Introduction to Seaborn for Enhanced Visuals
Seaborn's Role: High-Level Interface
Setting Seaborn Styles and Contexts
Seaborn Color Palettes
Creating Scatter Plots with Seaborn (scatterplot)
Creating Line Plots with Seaborn (lineplot)
Understanding Seaborn's Data Input Requirements
Practice: Recreating Plots with Seaborn
Quiz for Chapter 4
Chapter 5: Visualizing Data Distributions with Seaborn
Histograms and Kernel Density Estimates (histplot, kdeplot)
Visualizing Joint Distributions (jointplot)
Box Plots for Summary Statistics (boxplot)
Violin Plots: Combining Box Plots and KDE (violinplot)
Understanding Distribution Plot Choices
Hands-on Practical: Plotting Distributions
Quiz for Chapter 5
Chapter 6: Plotting Categorical Data using Seaborn
Understanding Categorical Data
Bar Plots for Aggregate Statistics (barplot)
Count Plots for Frequencies (countplot)
Box Plots for Categorical Variables (boxplot)
Strip Plots and Swarm Plots for Individual Points (stripplot, swarmplot)
Point Plots for Trends (pointplot)
Hands-on Practical: Visualizing Categorical Features
Quiz for Chapter 6
Chapter 7: Customizing and Saving Visualizations
Adding Text and Annotations to Plots
Managing Legends
Adjusting Figure Size and Layout
Using Matplotlib Object-Oriented API for Finer Control
Applying Seaborn Themes
Saving Plots to Files (PNG, JPG, PDF, SVG)
Choosing the Right File Format
Practice: Polishing Your Plots
Quiz for Chapter 7
Chapter 8: Plotting with Pandas DataFrames
Brief Introduction to Pandas Series and DataFrames
Loading Data into a DataFrame
Basic Plotting Directly from DataFrames
Using Matplotlib with DataFrame Columns
Using Seaborn with DataFrames
Filtering and Preparing Data for Plotting
Hands-on Practical: Visualizing Data from a File
Quiz for Chapter 8

Histograms for Distributions

While bar charts help compare discrete categories, we often need to understand the distribution of a single continuous numerical variable. How are the values spread out? Are they clustered around a central point, or are they more evenly distributed? This is where histograms come in handy.

A histogram is a graphical representation that organizes a group of data points into specified ranges or intervals, called bins. It looks similar to a bar chart, but there's a significant difference: a histogram visualizes the frequency distribution of continuous or discrete numerical data, whereas a bar chart compares categorical data. The height of each bar in a histogram represents the number of data points (frequency) that fall within that specific bin.

Think of it like sorting values into buckets. If you have a list of student heights, a histogram could show you how many students fall into the height range 150-160 cm, how many are between 160-170 cm, and so on. This gives you a visual sense of the data's shape, center, and spread.

Creating a Histogram with plt.hist()

Matplotlib provides the plt.hist() function to create histograms easily. Its most basic usage requires just one argument: the sequence of data (like a list or a NumPy array) whose distribution you want to visualize.

Let's generate some sample data representing, for example, the scores of students on a test, and then plot a histogram. We'll use NumPy to create some normally distributed random data for this illustration.

import matplotlib.pyplot as plt
import numpy as np

# Generate some sample data (e.g., test scores)
# Using a normal distribution centered around 70 with a standard deviation of 10
np.random.seed(42) # for reproducible results
scores = np.random.normal(loc=70, scale=10, size=200)

# Create the histogram
plt.figure(figsize=(8, 5)) # Optional: Adjust figure size
plt.hist(scores, color='#339af0') # Pass the data to plt.hist()

# Add labels and title for clarity
plt.xlabel("Test Score")
plt.ylabel("Number of Students (Frequency)")
plt.title("Distribution of Test Scores")

# Display the plot
plt.show()

This code first generates 200 random scores that roughly follow a normal distribution. Then, plt.hist(scores, color='#339af0') takes this data and automatically calculates the bin ranges and frequencies to draw the histogram bars. We've also added labels and a title using functions like plt.xlabel(), plt.ylabel(), and plt.title(), which you learned about previously. The result is a plot showing how many student scores fall into various score intervals.

A histogram showing the distribution of 200 simulated test scores. The x-axis shows the score ranges (bins), and the y-axis shows the number of students whose scores fall into each range. The shape approximates a bell curve (normal distribution).

Why Use a Histogram?

Histograms are fundamental tools in exploratory data analysis. They help you quickly grasp:

  • Shape of the Data: Is the data symmetric (like a bell curve), skewed to one side (left or right), bimodal (two peaks), or uniform?
  • Central Tendency: Where do most of the values cluster? You can visually estimate the approximate center of the data.
  • Spread or Variability: Are the values tightly packed around the center or widely dispersed across the range?
  • Outliers: Are there bars far separated from the main body of the histogram, suggesting unusual values?

By default, plt.hist() automatically determines the number and width of the bins based on the input data. While this automatic selection is often adequate, the choice of bins can significantly influence the histogram's appearance and potentially alter your interpretation of the data's distribution. We will look into how to control the number and width of these bins in the next section.

  • matplotlib.pyplot.hist, Matplotlib Development Team, 2024 - Official documentation for the Matplotlib hist function, detailing its arguments, usage, and examples for creating histograms.
  • Fundamentals of Data Visualization, Claus O. Wilke, 2019 (O'Reilly Media) - A guide covering the principles and practices for creating accurate and effective data visualizations, including a section on histograms and their proper interpretation.
  • Practical Statistics for Data Scientists: 50+ Essential Concepts Using R and Python, Peter Bruce, Andrew Bruce, and Peter Gedeck, 2020 (O'Reilly Media) - This book provides a data-centric introduction to statistical methods, discussing the importance of visualizing data distributions with tools like histograms for exploratory data analysis.

© 2026 ApX Machine Learning