Mathew K Analytics

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…

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 .ipynb

Data 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))
(150, 5)
   sepal_length  sepal_width  petal_length  petal_width species
0           5.1          3.5           1.4          0.2  setosa
1           4.9          3.0           1.4          0.2  setosa
2           4.7          3.2           1.3          0.2  setosa

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()
No description has been provided for this image

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()
No description has been provided for this image
sns.kdeplot(data=df, x='petal_length', hue='species', fill=True)
plt.title('Real Petal Length Density, by Species')
plt.show()
No description has been provided for this image

Part 3: boxplot and violinplot#

sns.boxplot(data=df, x='species', y='petal_length')
plt.title('Real Petal Length by Species')
plt.show()
No description has been provided for this image
sns.violinplot(data=df, x='species', y='sepal_width')
plt.title('Real Sepal Width by Species')
plt.show()
No description has been provided for this image

Part 4: pairplot#

sns.pairplot(df, hue='species')
plt.show()
No description has been provided for this image

Part 5: Correlation Heatmap#

numeric_df = df.drop(columns='species')
corr = numeric_df.corr()
print(corr.round(2))
              sepal_length  sepal_width  petal_length  petal_width
sepal_length          1.00        -0.12          0.87         0.82
sepal_width          -0.12         1.00         -0.43        -0.37
petal_length          0.87        -0.43          1.00         0.96
petal_width           0.82        -0.37          0.96         1.00
sns.heatmap(corr, annot=True, cmap='coolwarm', center=0)
plt.title('Real Correlation Between Iris Measurements')
plt.show()
No description has been provided for this image

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()
No description has been provided for this image

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.

  1. What does the hue='species' argument do in sns.scatterplot?

  2. Which seaborn function compares a numeric column across categories using quartiles and outliers?

  3. What does sns.pairplot(df, hue='species') produce?

  4. What must you compute first before drawing sns.heatmap(..., annot=True) of relationships between columns?

  5. What does sns.set_theme(style='whitegrid') do?

💻 Practice exercises

Try each one in the notebook or in your own editor before opening the solution.

  1. 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')
    
  2. 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))
    
  3. 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.