Mathew K Analytics

Lesson 44 · Data visualisation in python

Build an Interactive Data Visualization App: Capstone Project Step-by-Step Guide

Welcome! In this project, you will learn how to build a basic interactive data visualization app using Python. You will work with real-world time series…

⬇ Download notebookOpen in Colab ↗

📓 Full notebook

Download .ipynb

Capstone Project: Build an Interactive Data Visualization App#

Welcome! In this project, you will learn how to build a basic interactive data visualization app using Python. You will work with real-world time series data, practice loading and plotting data, and create your own mini-app.

By the end, you will have a hands-on introduction to interactive visualizations, and ways to explore datasets on your own.

Let us get started!

What You Need#

  • Python (3.7 or newer recommended)
  • Packages: pandas, matplotlib, plotly

Do not worry if you have never used these before; we will walk through everything step by step.

# Setup: Install required packages if needed
import warnings; warnings.filterwarnings('ignore')
try:
    import pandas as pd
except ImportError:
    !pip install pandas
    import pandas as pd
try:
    import matplotlib.pyplot as plt
except ImportError:
    !pip install matplotlib
    import matplotlib.pyplot as plt
try:
    import plotly.express as px
except ImportError:
    !pip install plotly
    import plotly.express as px

    

Data Setup#

We will use real monthly airline passenger data for this capstone.

This dataset is from 1949 to 1960 and is a popular example for time series.

Let us load it and take a peek.

# Data setup: Load the airline passengers dataset
url = "https://raw.githubusercontent.com/jbrownlee/Datasets/master/airline-passengers.csv"
df = pd.read_csv(url)
print('Shape:', df.shape)
df.head()
Shape: (144, 2)
Month Passengers
0 1949-01 112
1 1949-02 118
2 1949-03 132
3 1949-04 129
4 1949-05 121
# Basic data checks
print('Columns:', df.columns.tolist())
print('Missing entries:', df.isnull().sum().sum())
Columns: ['Month', 'Passengers']
Missing entries: 0

Lets Visualize: Simple Line Plot#

Time series data shows how things change over time.

A line plot helps us see trends and patterns.

Now, let us make our first visualization!

# Plot the data
plt.figure(figsize=(10,4))
plt.plot(df["Month"], df["Passengers"], marker="o")
plt.title("Monthly Airline Passengers (1949-1960)")
plt.xlabel("Month")
plt.ylabel("Passengers")
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()
No description has been provided for this image
# Use Plotly for interactive plotting
fig = px.line(df, x="Month", y="Passengers", title="Monthly Airline Passengers (Interactive)")
fig.show()

Exploring Data with Slices#

Sometimes, you want to focus on just part of the data, like a specific year.

Let us see how to select a time range.

# View data for 1955 only
df["Year"] = df["Month"].str[:4]
df_1955 = df[df["Year"] == "1955"]
print(df_1955)
      Month  Passengers  Year
72  1955-01         242  1955
73  1955-02         233  1955
74  1955-03         267  1955
75  1955-04         269  1955
76  1955-05         270  1955
77  1955-06         315  1955
78  1955-07         364  1955
79  1955-08         347  1955
80  1955-09         312  1955
81  1955-10         274  1955
82  1955-11         237  1955
83  1955-12         278  1955
# Input example: Let user pick a year
user_year = input("Enter a year to view (1949-1960): ")
filtered = df[df["Year"] == user_year]
print(filtered)
       Month  Passengers  Year
108  1958-01         340  1958
109  1958-02         318  1958
110  1958-03         362  1958
111  1958-04         348  1958
112  1958-05         363  1958
113  1958-06         435  1958
114  1958-07         491  1958
115  1958-08         505  1958
116  1958-09         404  1958
117  1958-10         359  1958
118  1958-11         310  1958
119  1958-12         337  1958

Keeping Data Safe: What Happens if I Pick a Year That Does Not Exist?#

Real data sometimes does not have records for every input.

Let us see what happens if you ask for a year not in the dataset.

# What if the year is missing?
wrong_year = input("Try entering a missing year, like 2025: ")
result = df[df["Year"] == wrong_year]
if result.empty:
    print("No data found for that year!")
else:
    print(result)
    
No data found for that year!

Mini App: Pick a Year and Plot That Slice#

Let us put what you have learned into an interactive function.

This function will ask for a year and plot just that year.

Ready?

# Function for year plot
def plot_year(year):
    slice = df[df["Year"] == year]
    if slice.empty:
        print("Sorry, no data for that year.")
        return
    plt.figure(figsize=(7,3))
    plt.plot(slice["Month"], slice["Passengers"], marker="o", color="green")
    plt.title(f"Airline Passengers in {year}")
    plt.xlabel("Month")
    plt.ylabel("Passengers")
    plt.xticks(rotation=45)
    plt.tight_layout()
    plt.show()
    
# Try the mini app
input_year = input("Type a valid year (1949-1960): ")
plot_year(input_year)
No description has been provided for this image
# Sorting and filtering: Find years with most growth
yearly_sum = df.groupby("Year")["Passengers"].sum().reset_index()
top = yearly_sum.sort_values("Passengers", ascending=False)
print(top.head())
    Year  Passengers
11  1960        5714
10  1959        5140
9   1958        4572
8   1957        4421
7   1956        3939
# Try a bar plot for total passengers by year
plt.figure(figsize=(8,4))
plt.bar(yearly_sum["Year"], yearly_sum["Passengers"], color="skyblue")
plt.title("Total Passengers by Year")
plt.xlabel("Year")
plt.ylabel("Passengers")
plt.tight_layout()
plt.show()
No description has been provided for this image

Experiment: Try Filtering Data by Value#

Want to see only months with over 400 passengers?

Let us filter the table to highlight those periods.

# Filter for months with more than 400 passengers
high_months = df[df["Passengers"] > 400]
print(high_months)
       Month  Passengers  Year
90   1956-07         413  1956
91   1956-08         405  1956
101  1957-06         422  1957
102  1957-07         465  1957
103  1957-08         467  1957
104  1957-09         404  1957
113  1958-06         435  1958
114  1958-07         491  1958
115  1958-08         505  1958
116  1958-09         404  1958
122  1959-03         406  1959
124  1959-05         420  1959
125  1959-06         472  1959
126  1959-07         548  1959
127  1959-08         559  1959
128  1959-09         463  1959
129  1959-10         407  1959
131  1959-12         405  1959
132  1960-01         417  1960
134  1960-03         419  1960
135  1960-04         461  1960
136  1960-05         472  1960
137  1960-06         535  1960
138  1960-07         622  1960
139  1960-08         606  1960
140  1960-09         508  1960
141  1960-10         461  1960
143  1960-12         432  1960
# Try a line plot of the filtered months
if not high_months.empty:
    plt.figure(figsize=(10,4))
    plt.plot(high_months["Month"], high_months["Passengers"], marker="o", color="red")
    plt.title("Months with More Than 400 Passengers")
    plt.xlabel("Month")
    plt.ylabel("Passengers")
    plt.xticks(rotation=45)
    plt.tight_layout()
    plt.show()
else:
    print("No months found with that many passengers.")
    
No description has been provided for this image
# Combining filters: Summer months over 400 passengers
summer_high = df[df["Month"].str[5:7].isin(["06", "07", "08"]) & (df["Passengers"] > 400)]
print(summer_high)
       Month  Passengers  Year
90   1956-07         413  1956
91   1956-08         405  1956
101  1957-06         422  1957
102  1957-07         465  1957
103  1957-08         467  1957
113  1958-06         435  1958
114  1958-07         491  1958
115  1958-08         505  1958
125  1959-06         472  1959
126  1959-07         548  1959
127  1959-08         559  1959
137  1960-06         535  1960
138  1960-07         622  1960
139  1960-08         606  1960

Challenge: Try Creating Your Own Filter#

Can you find months where the passenger number is increasing three months in a row?

Try writing code with a friend, or give it a go below!

# Trend finder: Three months up in a row
df['Increase'] = (df['Passengers'] > df['Passengers'].shift(1)) & (df['Passengers'].shift(1) > df['Passengers'].shift(2))
up_streaks = df[df['Increase']]
print(up_streaks[['Month', 'Passengers']])
       Month  Passengers
2    1949-03         132
6    1949-07         148
14   1950-03         141
18   1950-07         170
24   1951-01         145
25   1951-02         150
26   1951-03         178
29   1951-06         178
30   1951-07         199
36   1952-01         171
37   1952-02         180
38   1952-03         193
41   1952-06         218
42   1952-07         230
43   1952-08         242
48   1953-01         196
54   1953-07         264
55   1953-08         272
60   1954-01         204
65   1954-06         264
66   1954-07         302
72   1955-01         242
75   1955-04         269
76   1955-05         270
77   1955-06         315
78   1955-07         364
84   1956-01         284
89   1956-06         374
90   1956-07         413
96   1957-01         315
101  1957-06         422
102  1957-07         465
103  1957-08         467
108  1958-01         340
113  1958-06         435
114  1958-07         491
115  1958-08         505
120  1959-01         360
125  1959-06         472
126  1959-07         548
127  1959-08         559
132  1960-01         417
135  1960-04         461
136  1960-05         472
137  1960-06         535
138  1960-07         622

Recap: What Did We Learn?#

  • How to set up Python for data visualization
  • How to download, load, and preview real data
  • Ways to filter, slice, and visualize time series
  • Interactive mini apps for exploring data
  • Tips for combining filters and finding patterns

You are ready to try new datasets on your own! Practice and experiment.

Try More, and Subscribe!#

If you enjoyed building this interactive app, please leave a comment below.

Subscribe to our channel for more fun beginner Python projects!

What real world data would you like to explore next? Let us know!

Found this useful?

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