Lesson 14 · Data visualisation in python
Introduction to Seaborn for Data Visualization with Built-in Python Datasets
In this lesson, we will explore Seaborn, a powerful Python library for visualizing data. You will learn how to load built-in datasets, create beautiful…
- CourseData visualisation in python
- Lesson14 of 34
- Video10 min
- FormatJupyter notebook · 16 code cells
What you'll learn
Data
No separate download needed — the notebook creates or downloads everything it uses.
📓 Full notebook
Download .ipynbWelcome to the Beginner's Guide to Seaborn!#
In this lesson, we will explore Seaborn, a powerful Python library for visualizing data.
You will learn how to load built-in datasets, create beautiful charts, and uncover patterns with simple code.
No prior experience needed. Let's get started!
# Let\'s set up our environment
import warnings
warnings.filterwarnings("ignore")
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
# These libraries will help us explore and plot our data
What is Seaborn?#
Seaborn is a library in Python designed to make data visualization simple and attractive.
It works well with pandas DataFrames, which are tables of rows and columns.
With just a few lines of code, you can see patterns in your data.
# Let\'s view Seaborn\'s built-in datasets
print(sns.get_dataset_names())
# Let\'s load the \"flights\" dataset
flights = sns.load_dataset("flights")
# Show the top 5 rows
flights.head()
Reading the flights table#
Each row in the table shows a month and year, along with the number of airline passengers.
This is a small time series, which means it shows numbers changing over time.
Next, we will look at the size and a simple preview.
# Show the shape: rows and columns
print("Shape:", flights.shape)
# Show quick summary of the dataset
print("\nInfo:")
flights.info()
# Plot how the number of passengers changes over time
plt.figure(figsize=(10,4))
sns.lineplot(data=flights, x="year", y="passengers")
plt.title("Passengers per Year")
plt.show()
Exploring Variables#
The 'flights' dataset has three columns: 'year', 'month', and 'passengers'.
'year' and 'month' tell us when. 'passengers' is a number count.
Understanding variables helps us choose visualizations.
# See all unique months in the dataset
print(flights["month"].unique())
# How many records for each month?
print(flights["month"].value_counts())
# Let\'s plot passengers per month for a year
data_1950 = flights[flights["year"]==1950]
sns.barplot(data=data_1950, x="month", y="passengers")
plt.title("Passengers per Month in 1950")
plt.show()
# What if we want to see change across all years by month?
pivot_data = flights.pivot(index="month", columns="year", values="passengers")
sns.heatmap(pivot_data, cmap="YlGnBu")
plt.title("Monthly Airline Passengers (1949-1960)")
plt.show()
# Check for any missing values in the dataset
print(flights.isnull().sum())
# Summary statistics for passengers
print(flights["passengers"].describe())
# Filter the data: find months with over 400 passengers
busy = flights[flights["passengers"]>400]
print(busy.head())
Combining Data#
You can join or combine datasets if you want to add new information.
For example, you could add a new column for holidays or special events.
Let's see a small example next.
# Add a new column: Is Summer?
flights["is_summer"] = flights["month"].isin(["June","July","August"])
print(flights[["month", "is_summer"]].head(12))
# Mini-project: Visualize only the summer months
summer_flights = flights[flights["is_summer"]]
sns.lineplot(data=summer_flights, x="year", y="passengers", ci=None)
plt.title("Summer Passengers Over the Years")
plt.show()
# Save a chart to a file
plt.figure(figsize=(8,4))
sns.lineplot(data=flights, x="year", y="passengers")
plt.title("Passengers per Year")
plt.savefig("passengers_per_year.png")
plt.close()
# Troubleshooting: What if you make a typo?
try:
sns.lineplot(data=flights, x="yeer", y="passengers")
except Exception as e:
print("Error:", e)
# User input: Let\'s let you pick a year to visualize!
user_year = input("Type a year between 1949 and 1960: ")
selected = flights[flights["year"]==int(user_year)]
sns.barplot(data=selected, x="month", y="passengers")
plt.title(f"Airline Passengers in {user_year}")
plt.show()
Recap and Practice#
You have learned how to use Seaborn to work with built-in datasets, make line and bar charts, and find data patterns.
Try exploring other datasets in Seaborn. Change filters and create your own plots.
Visualization helps you see stories in data more clearly.
Thanks for Learning with Us!#
If you enjoyed this lesson, hit Like and Subscribe for more Python tips and tutorials.
Happy coding and keep exploring data with Seaborn!
See you next time!
Found this useful?
All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.



