Thinking Like a Programmer•Interactive notebook lesson•Free
matplotlib seaborn projects
No account is required. Learn the material, try the examples, and mark it complete locally when you are ready to move on.
Matplotlib and Seaborn Complete Guide
This notebook teaches data visualization in Python using:
- Matplotlib
- Seaborn
It also includes two real-world mini projects:
- Housing price visualization (housing.csv)
- Telecom churn analysis (TelecomCustomerChurn.csv)
Libraries used:
- pandas
- numpy
- matplotlib
- seaborn
Codepython
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_theme(style="whitegrid")
print("Libraries loaded")
Output
Libraries loaded
Basic Line Plot
Codepython
x = np.arange(0,10)
y = x**2
plt.figure()
plt.plot(x,y)
plt.title("Line Plot Example")
plt.xlabel("X")
plt.ylabel("Y")
plt.show()
Output
Scatter Plot
Codepython
x = np.arange(0,50)
y = np.arange(50,100)
plt.scatter(x,y)
plt.title("Scatter Plot")
plt.show()
Output
Bar Chart
Codepython
categories = ["A","B","C"]
values = [10,20,15]
plt.bar(categories,values)
plt.title("Bar Chart")
plt.show()
Output
Histogram
Codepython
data = np.random.randn(1000)
plt.hist(data,bins=30)
plt.title("Histogram")
plt.show()
Output
Multiple Plots
Codepython
x = np.linspace(0,10,100)
plt.subplot(1,2,1)
plt.plot(x,np.sin(x))
plt.subplot(1,2,2)
plt.plot(x,np.cos(x))
plt.show()
Output
Seaborn Visualizations
Distribution Plot
Codepython
data = np.random.randn(500)
sns.histplot(data,kde=True)
plt.show()
Output
Box Plot
Codepython
data = pd.DataFrame({
"category":["A","A","B","B","C","C"],
"value":[5,7,3,4,6,8]
})
sns.boxplot(x="category",y="value",data=data)
plt.show()
Output
Heatmap
Codepython
matrix = np.random.rand(5,5)
sns.heatmap(matrix,annot=True)
plt.show()
Output
Pairplot
Codepython
df = pd.DataFrame(np.random.rand(100,4),columns=["A","B","C","D"])
sns.pairplot(df)
Output
<seaborn.axisgrid.PairGrid at 0x7f52499dee30>Project 1: Housing Data Visualization
Dataset: housing.csv
Goals:
- Explore price distribution
- Analyze correlations
- Visualize relationships
Codepython
housing = pd.read_csv("../../datasets/housing.csv")
housing.head()
Output
longitude latitude housing_median_age total_rooms total_bedrooms \
0 -122.23 37.88 41.0 880.0 129.0
1 -122.22 37.86 21.0 7099.0 1106.0
2 -122.24 37.85 52.0 1467.0 190.0
3 -122.25 37.85 52.0 1274.0 235.0
4 -122.25 37.85 52.0 1627.0 280.0
population households median_income median_house_value ocean_proximity
0 322.0 126.0 8.3252 452600.0 NEAR BAY
1 2401.0 1138.0 8.3014 358500.0 NEAR BAY
2 496.0 177.0 7.2574 352100.0 NEAR BAY
3 558.0 219.0 5.6431 341300.0 NEAR BAY
4 565.0 259.0 3.8462 342200.0 NEAR BAY Codepython
plt.hist(housing.select_dtypes(include=np.number).iloc[:,0], bins=30)
plt.title("Distribution Example")
plt.show()
Output
Codepython
corr = housing.corr(numeric_only=True)
plt.figure(figsize=(10,8))
sns.heatmap(corr,cmap="coolwarm")
plt.title("Correlation Heatmap")
plt.show()
Output
Codepython
num_cols = housing.select_dtypes(include=np.number).columns[:4]
sns.pairplot(housing[num_cols])
Output
<seaborn.axisgrid.PairGrid at 0x7f523eb961d0>Project 2: Telecom Customer Churn Visualization
Dataset: TelecomCustomerChurn.csv
Goals:
- Understand churn distribution
- Identify patterns in features
Codepython
churn = pd.read_csv("../../datasets/TelecomCustomerChurn.csv")
churn.head()
Output
customerID Gender SeniorCitizen Partner Dependents Tenure PhoneService \
0 7590-VHVEG Female 0 Yes No 1 No
1 5575-GNVDE Male 0 No No 34 Yes
2 3668-QPYBK Male 0 No No 2 Yes
3 7795-CFOCW Male 0 No No 45 No
4 9237-HQITU Female 0 No No 2 Yes
MultipleLines InternetService OnlineSecurity ... DeviceProtection \
0 No DSL No ... No
1 No DSL Yes ... Yes
2 No DSL Yes ... No
3 No DSL Yes ... Yes
4 No Fiber optic No ... No
TechSupport StreamingTV StreamingMovies Contract PaperlessBilling \
0 No No No Monthly Yes
1 No No No One year No
2 No No No Monthly Yes
3 Yes No No One year No
4 No No No Monthly Yes
PaymentMethod MonthlyCharges TotalCharges Churn
0 Manual 29.85 29.85 No
1 Manual 56.95 1889.5 No
2 Manual 53.85 108.15 Yes
3 Bank transfer (automatic) 42.30 1840.75 No
4 Manual 70.70 151.65 Yes
[5 rows x 21 columns]Codepython
sns.countplot(x="Churn", data=churn)
plt.title("Churn Distribution")
plt.show()
Output
Codepython
numeric_cols = churn.select_dtypes(include=np.number).columns
churn[numeric_cols].hist(figsize=(10,8))
plt.show()
Output
Codepython
plt.figure(figsize=(10,8))
sns.heatmap(churn.corr(numeric_only=True), cmap="viridis")
plt.title("Feature Correlations")
plt.show()
Output
Finished this lesson?
Completion is saved in this browser without creating an account.