Lesson 14 · Data analytics zero to hero
Beautiful Charts with Seaborn in Python | Data Analytics #14
Seaborn, a higher-level charting library built directly on top of Matplotlib, for faster, more polished statistical charts. We're using the real Iris…
- CourseData analytics zero to hero
- Lesson14 of 30
- Video12 min
- FormatJupyter notebook · 10 code cells
- Data1 dataset
What you'll learn
Datasets used in this lesson
Save these next to the notebook. In Google Colab, upload them with the 📁 icon on the left first.
📓 Full notebook
Download .ipynbData Analytics Zero to Hero, Video 14: Data Visualization with Seaborn#
- Video fourteen of the 30-part series: Seaborn, a higher-level charting library built directly on top of Matplotlib, for faster, more polished statistical charts.
- We're using the real Iris dataset, Ronald Fisher's classic 1936 flower measurements, one of the most famous real datasets in all of statistics.
- Let's jump straight in.
Before You Start#
- Open a new Jupyter Notebook in VS Code and select your Python interpreter as the kernel.
- Install Seaborn if you haven't already:
pip install seaborn. - Place iris.csv in the same folder as this notebook.
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
df = pd.read_csv('iris.csv')
print(df.shape)
print(df.head(3))
Part 1: scatterplot with hue#
sns.scatterplot(data=df, x='petal_length', y='petal_width', hue='species')
plt.title('Real Petal Length vs. Width, by Species')
plt.show()
Part 2: Distributions with histplot and kdeplot#
sns.histplot(data=df, x='sepal_length', hue='species', kde=True)
plt.title('Real Sepal Length Distribution, by Species')
plt.show()
sns.kdeplot(data=df, x='petal_length', hue='species', fill=True)
plt.title('Real Petal Length Density, by Species')
plt.show()
Part 3: boxplot and violinplot#
sns.boxplot(data=df, x='species', y='petal_length')
plt.title('Real Petal Length by Species')
plt.show()
sns.violinplot(data=df, x='species', y='sepal_width')
plt.title('Real Sepal Width by Species')
plt.show()
Part 4: pairplot#
sns.pairplot(df, hue='species')
plt.show()
Part 5: Correlation Heatmap#
numeric_df = df.drop(columns='species')
corr = numeric_df.corr()
print(corr.round(2))
sns.heatmap(corr, annot=True, cmap='coolwarm', center=0)
plt.title('Real Correlation Between Iris Measurements')
plt.show()
A Note on Styling#
sns.set_theme(style='whitegrid')
sns.scatterplot(data=df, x='sepal_length', y='sepal_width', hue='species')
plt.title('Themed Real Scatter Plot')
plt.show()
Wrap-Up: What You Learned#
- scatterplot with hue, for fast, automatically colored and legended scatter plots.
- histplot and kdeplot, for visualizing distributions, split by category.
- boxplot and violinplot, for comparing a numeric column across categories.
- pairplot, for surveying every numeric relationship in a dataset in one call.
- A correlation heatmap, for scanning relationships between many columns at once.
- set_theme, for a consistent, polished look across every chart.
- All of it on the real, famous Iris dataset. This wraps up the visualization block. Video fifteen builds an interactive dashboard with Streamlit, turning charts like these into a real shareable app. Subscribe so it lands automatically see you there.
🧠 Test yourself
Pick an answer to check it. Your best score is saved in this browser.
What does the
hue='species'argument do insns.scatterplot?hue maps a categorical column to color automatically, with a legend.
Which seaborn function compares a numeric column across categories using quartiles and outliers?
A box plot shows the median, quartiles, and outliers for each category.
What does
sns.pairplot(df, hue='species')produce?pairplot surveys all numeric relationships at once, with distributions on the diagonal.
What must you compute first before drawing
sns.heatmap(..., annot=True)of relationships between columns?A correlation heatmap displays the matrix from
numeric_df.corr(). Drop non-numeric columns like species first.What does
sns.set_theme(style='whitegrid')do?set_theme sets global styling so every later chart has the same polished look.
💻 Practice exercises
Try each one in the notebook or in your own editor before opening the solution.
Create a small DataFrame with petal_length, petal_width and species (2 species) and draw a seaborn scatterplot with hue. Save it.
💡 Hint
sns.scatterplot(data=df, x=..., y=..., hue='species').
✅ Show solution
import matplotlib matplotlib.use('Agg') import pandas as pd import seaborn as sns import matplotlib.pyplot as plt df = pd.DataFrame({ 'petal_length': [1.4, 1.3, 4.7, 4.5, 1.5, 4.9], 'petal_width': [0.2, 0.2, 1.4, 1.5, 0.3, 1.5], 'species': ['setosa', 'setosa', 'versicolor', 'versicolor', 'setosa', 'versicolor'] }) sns.scatterplot(data=df, x='petal_length', y='petal_width', hue='species') plt.title('Petal length vs width') plt.savefig('scatter.png')
Draw a boxplot of petal_length per species from a small DataFrame and print the correlation matrix of the numeric columns, rounded to 2 decimals.
💡 Hint
Use sns.boxplot(data=df, x='species', y='petal_length') and df.drop(columns='species').corr().
✅ Show solution
import matplotlib matplotlib.use('Agg') import pandas as pd import seaborn as sns import matplotlib.pyplot as plt df = pd.DataFrame({ 'petal_length': [1.4, 1.3, 4.7, 4.5, 1.5, 4.9], 'petal_width': [0.2, 0.2, 1.4, 1.5, 0.3, 1.5], 'species': ['setosa', 'setosa', 'versicolor', 'versicolor', 'setosa', 'versicolor'] }) sns.boxplot(data=df, x='species', y='petal_length') plt.savefig('box.png') plt.close() print(df.drop(columns='species').corr().round(2))
Make a correlation heatmap (annot=True, cmap='coolwarm', center=0) for three numeric columns and a histplot with kde=True split by species, after setting the whitegrid theme.
💡 Hint
Set the theme first, then call sns.heatmap(corr, ...) and sns.histplot(..., hue='species', kde=True).
✅ Show solution
import matplotlib matplotlib.use('Agg') import pandas as pd import seaborn as sns import matplotlib.pyplot as plt sns.set_theme(style='whitegrid') df = pd.DataFrame({ 'sepal_length': [5.1, 4.9, 7.0, 6.4, 5.0, 6.9, 5.4, 6.3], 'petal_length': [1.4, 1.4, 4.7, 4.5, 1.5, 4.9, 1.7, 4.4], 'petal_width': [0.2, 0.2, 1.4, 1.5, 0.2, 1.5, 0.4, 1.3], 'species': ['setosa', 'setosa', 'versicolor', 'versicolor', 'setosa', 'versicolor', 'setosa', 'versicolor'] }) corr = df.drop(columns='species').corr() sns.heatmap(corr, annot=True, cmap='coolwarm', center=0) plt.savefig('heat.png') plt.close() sns.histplot(data=df, x='sepal_length', hue='species', kde=True) plt.savefig('hist.png')
Found this useful?
All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.



