Mathew K Analytics

Lesson 6 · Probability and Statistics in python

Fundamentals of Data Visualization: Histograms, Boxplots, and Scatterplots Explained

Welcome! In this lesson, you will learn how to create simple and powerful graphs. We will use real datasets to make concepts easy and practical. By the end,…

⬇ Download notebookOpen in Colab ↗

What you'll learn

Data

No separate download needed — the notebook creates or downloads everything it uses.

📓 Full notebook

Download .ipynb

Data Visualization Basics: Histograms, Boxplots, Scatterplots#

Welcome! In this lesson, you will learn how to create simple and powerful graphs.

We will use real datasets to make concepts easy and practical.

By the end, you will know how to explore and share data with clear charts.

Let us get started!

import warnings; warnings.filterwarnings("ignore")
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

# Data setup
df = sns.load_dataset("tips")
print("Shape:", df.shape)
df.head()
Shape: (244, 7)
total_bill tip sex smoker day time size
0 16.99 1.01 Female No Sun Dinner 2
1 10.34 1.66 Male No Sun Dinner 3
2 21.01 3.50 Male No Sun Dinner 3
3 23.68 3.31 Male No Sun Dinner 2
4 24.59 3.61 Female No Sun Dinner 4

What is a Histogram?#

A histogram is a bar graph that shows how many data points fall within certain ranges.

It is great for seeing patterns, like how spread out tips are or which values are most common.

# Let us make a histogram of the 'tip' column.
plt.figure(figsize=(8,4))
sns.histplot(df['tip'], bins=10, kde=True, color='skyblue', edgecolor='black')
plt.title('Histogram of Tip Amounts')
plt.xlabel('Tip Amount ($)')
plt.ylabel('Count')
plt.show()
No description has been provided for this image

Why Are Histograms Useful?#

Histograms help us spot patterns quickly, like whether tips are usually small, large, or somewhere in between.

They are helpful for finding odd or surprising values too.

# Let us look at another column, the total bill amount.
sns.histplot(df['total_bill'], bins=15, kde=True, color='lightgreen', edgecolor='black')
plt.title('Distribution of Total Bill Amount')
plt.xlabel('Total Bill ($)')
plt.ylabel('Count')
plt.show()
No description has been provided for this image

What is a Boxplot?#

A boxplot is a smart graph that summarizes data using minimum, lower quartile, median, upper quartile, and maximum.

It quickly shows if data is balanced, if there are outliers, or if most values are high or low.

# Boxplot of tips to see summary statistics and spot outliers
plt.figure(figsize=(7,3))
sns.boxplot(x=df['tip'], color='orange')
plt.title('Boxplot of Tips')
plt.xlabel('Tip Amount ($)')
plt.show()
No description has been provided for this image
# Compare tips by day using boxplots
plt.figure(figsize=(8,4))
sns.boxplot(x='day', y='tip', data=df, palette="Set2")
plt.title('Tips by Day of Week')
plt.xlabel('Day of Week')
plt.ylabel('Tip Amount ($)')
plt.show()
No description has been provided for this image

What is a Scatterplot?#

A scatterplot lets us see the relationship between two variables.

If points go up together, that means as one value increases, the other tends to increase.

Scatterplots are great to spot clusters, trends, or patterns.

# Scatterplot of total bill vs tip
plt.figure(figsize=(8,5))
sns.scatterplot(x='total_bill', y='tip', data=df, hue='sex', style='time', palette='Set1')
plt.title('Tip Amount vs Total Bill')
plt.xlabel('Total Bill ($)')
plt.ylabel('Tip Amount ($)')
plt.legend(title='Sex / Meal Time')
plt.show()
No description has been provided for this image
# Add a trend line with Seaborn's regplot
sns.regplot(x='total_bill', y='tip', data=df, scatter_kws={'alpha':0.6}, line_kws={'color':'red'})
plt.title('Tip vs Total Bill with Trend Line')
plt.xlabel('Total Bill ($)')
plt.ylabel('Tip Amount ($)')
plt.show()
No description has been provided for this image

Handling Missing Data for Visualization#

Sometimes data has missing values.

It is important to handle or fill these so charts do not break.

Let us check for and fix missing values.

# Check for missing values
df.isnull().sum()
total_bill    0
tip           0
sex           0
smoker        0
day           0
time          0
size          0
dtype: int64
# Fill missing values, just in case
df_filled = df.fillna(df.mean(numeric_only=True))
df = df_filled.copy()
df.isnull().sum()
total_bill    0
tip           0
sex           0
smoker        0
day           0
time          0
size          0
dtype: int64

Compare Distributions: Multiple Groups#

We can see differences in data by splitting into groups.

Let us look at how tips differ by gender using histograms and boxplots.

# Histogram: Tips by Gender
plt.figure(figsize=(8,4))
sns.histplot(data=df, x='tip', hue='sex', bins=10, kde=True, palette='husl', alpha=0.7)
plt.title('Tip Amounts by Gender')
plt.xlabel('Tip Amount ($)')
plt.ylabel('Count')
plt.show()
No description has been provided for this image
# Boxplot: Tips by Smoker Status
plt.figure(figsize=(7,3))
sns.boxplot(x='smoker', y='tip', data=df, palette='cool')
plt.title('Tip Amounts by Smoker Status')
plt.xlabel('Smoker')
plt.ylabel('Tip Amount ($)')
plt.show()
No description has been provided for this image

Customizing Plots: Style and Context#

You can easily change the look of plots with Seaborn's built-in themes.

Let us see how a simple setting can make charts look different.

# Set a new style for all plots in the notebook
sns.set_style('darkgrid')
plt.figure(figsize=(8,4))
sns.histplot(df['tip'], bins=10, kde=True, color='purple')
plt.title('Histogram with Darkgrid Style')
plt.xlabel('Tip Amount ($)')
plt.show()
No description has been provided for this image

Challenge: Your Turn!#

Try creating a scatterplot or boxplot using two different columns or groups.

Can you find an interesting pattern?

Try customizing the color or style as you wish.

# Mini-project: Explore all columns visually
for col in ['total_bill', 'tip', 'size']:
    plt.figure(figsize=(7,3))
    sns.histplot(df[col], bins=10, color='steelblue', kde=True, edgecolor='black')
    plt.title(f'Distribution of {col.title()}')
    plt.xlabel(col.title())
    plt.ylabel('Count')
    plt.show()
    
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image

Recap: What Did We Learn?#

You learned how to draw histograms for distributions, boxplots for summaries, and scatterplots to see relationships.

You saw how grouping and style help bring your findings to life.

Now you have the power to dig into any dataset visually!

Next Steps and Call to Action#

Try using these plots on your own data or another seaborn example.

Tell us in the YouTube comments what you would like to visualize next!

Like, subscribe, and share if you found this lesson helpful.

Found this useful?

All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.