How to Use Matplotlib and Seaborn Together in One Notebook

Matplotlib and Seaborn are often used together in data analysis notebooks.



  • Matplotlib gives you full control over plots.

  • Seaborn makes statistical visualizations cleaner and easier to create.

Since Seaborn is built on top of Matplotlib, they work seamlessly together.


Install the Libraries

pip install matplotlib seaborn

Import Both Libraries

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

Load a Dataset

import pandas as pd
from google.colab import files

# Upload CSV file
uploaded = files.upload()

# Get uploaded file name
file_name = list(uploaded.keys())[0]

# Read the CSV file, skipping the initial metadata rows
df = pd.read_csv(file_name, skiprows=4)

# Preview the data
print(df.head())



Create a Seaborn Plot

Seaborn makes statistical plots very simple.

import seaborn as sns

sns.histplot(df['2022'])
plt.show()



Customize Using Matplotlib

You can still use Matplotlib functions to control titles, labels, and figure sizes.

import seaborn as sns
import matplotlib.pyplot as plt

plt.figure(figsize=(8,5))

sns.histplot(df['2022'])

plt.title('GDP per capita (2022) Distribution')
plt.xlabel('GDP per capita (2022)')
plt.ylabel('Frequency')

plt.show()



This is the most common workflow:

  • Seaborn creates the chart

  • Matplotlib customizes it


Example: Scatter Plot With Seaborn and Matplotlib

import seaborn as sns
import matplotlib.pyplot as plt

plt.figure(figsize=(7,5))

sns.scatterplot(
    data=df,
    x='1960',
    y='2022'
)

plt.title('GDP per capita (1960) vs GDP per capita (2022)')
plt.xlabel('GDP per capita (1960)')
plt.ylabel('GDP per capita (2022)')

plt.show()



Why Use Both Together?

Using both libraries gives you:

  • cleaner charts,

  • advanced customization,

  • better statistical visualizations,

  • and publication-quality plots.

This combination is standard in:

  • data science,

  • business intelligence,

  • machine learning,

  • and analytics workflows.





Comments

Popular posts from this blog

How to Filter Rows Using Boolean Indexing in Pandas (Afrobarometer Kenya Dataset)

How to Decide Whether to Drop or Fill Missing Value

How to create your first line chart with World Bank Kenya GDP data