top of page

How to Visualize Data with Matplotlib and Seaborn 

An essential component of machine learning and data analysis is data visualization. It facilitates the comprehension of trends, correlations, and patterns in the data. Matplotlib and Seaborn are two of the most popular Python visualization libraries. While Seaborn expands upon Matplotlib and delivers more sophisticated, visually appealing statistical visuals, Matplotlib gives basic plotting capabilities.


In this blog, we'll look at how to use Seaborn and Matplotlib to make different kinds of plots, alter them, and make them easier to read for deeper insights.



visualization

Why Use Matplotlib and Seaborn?


Matplotlib is a low-level visualization library that provides full control over plot customization. It is frequently used to produce interactive, animated, and static visualizations. Contrarily, Seaborn is based on Matplotlib and comes with pre-installed themes and statistical visuals that make creating intricate visualizations simpler.


Key Benefits:

  • Matplotlib: Incredibly adaptable, ideal for both simple and intricate plots.

  • Seaborn: Makes it easier to create visually stunning statistics charts.


When combined, they provide strong instruments for producing perceptive and eye-catching data visualizations.


Installing and Importing Libraries

To use Matplotlib and Seaborn, you first need to install them. If you haven't already, install them using:

pip install matplotlib

pip install seaborn


Then, import the necessary libraries:

import matplotlib.pyplot as plt

import seaborn as sns

import numpy as np

import pandas as pd


Basic Plotting with Matplotlib


Matplotlib follows an object-oriented approach, allowing you to create and customize figures and axes. The most commonly used function is plt.plot(), which creates a simple line plot.

x = np.linspace(0, 10, 100)

y = np.sin(x)


plt.plot(x, y)

plt.title("Simple Line Plot")

plt.xlabel("X-axis")

plt.ylabel("Y-axis")

plt.show()

This code generates a basic sine wave plot as shown below.


Common Matplotlib Visualizations


1. Line Plot

A line plot is useful for showing trends over time.

x = np.arange(1, 11)

y = x ** 2


plt.plot(x, y, marker='o', linestyle='--', color='r')

plt.title("Line Plot Example")

plt.xlabel("X values")

plt.ylabel("Y values")

plt.grid(True)

plt.show()

This code generates a plot as shown below.


2. Bar Chart

A bar chart is used to compare categorical data.

categories = ['A', 'B', 'C', 'D']

values = [10, 15, 7, 12]


plt.bar(categories, values, color='blue')

plt.title("Bar Chart Example")

plt.xlabel("Categories")

plt.ylabel("Values")

plt.show()

This code generates a plot as shown below.


3. Scatter Plot

A scatter plot helps visualize relationships between two numerical variables.

x = np.random.rand(50)

y = np.random.rand(50)


plt.scatter(x, y, color='green', marker='o')

plt.title("Scatter Plot Example")

plt.xlabel("X values")

plt.ylabel("Y values")

plt.show()

This code generates a plot as shown below.


4. Histogram

A histogram shows the distribution of a dataset.

data = np.random.randn(1000)


plt.hist(data, bins=30, color='purple', alpha=0.7)

plt.title("Histogram Example")

plt.xlabel("Value")

plt.ylabel("Frequency")

plt.show()

This code generates a plot as shown below.


Advanced Visualizations with Seaborn


Seaborn simplifies the process of creating visually appealing statistical plots. It provides various built-in themes and functions for advanced plotting.


1. Line Plot with Seaborn

Seaborn's lineplot() function is useful for visualizing trends.


sns.set_theme(style="darkgrid")

x = np.linspace(0, 10, 100)

y = np.sin(x)


sns.lineplot(x=x, y=y)

plt.title("Seaborn Line Plot")

plt.show()

This code generates a plot as shown below.


2. Bar Chart

Seaborn makes it easy to visualize categorical data with the barplot() function.


data = pd.DataFrame({'Category': ['A', 'B', 'C', 'D'], 'Values': [10, 15, 7, 12]})

sns.barplot(x='Category', y='Values', data=data, palette="Blues")

plt.title("Seaborn Bar Chart")

plt.show()


This code generates a plot as shown below.


3. Scatter Plot with Regression Line

Seaborn provides regplot() to add regression lines to scatter plots.


tips = sns.load_dataset("tips")

sns.regplot(x="total_bill", y="tip", data=tips)

plt.title("Scatter Plot with Regression Line")

plt.show()


This code generates a plot as shown below.


4. Pair Plot

A pair plot is used to visualize relationships between multiple numerical variables in a dataset.


iris = sns.load_dataset("iris")

sns.pairplot(iris, hue="species", palette="coolwarm")

plt.title("Pair Plot Example")

plt.show()


This code generates a plot as shown below.


5. Heatmap

A heatmap is used to visualize correlations between numerical features.


import seaborn as sns

import matplotlib.pyplot as plt


# Load dataset

tips = sns.load_dataset("tips")


# Select only numeric columns

correlation = tips.select_dtypes(include=['number']).corr()


# Create heatmap

sns.heatmap(correlation, annot=True, cmap="coolwarm", linewidths=0.5)

plt.title("Heatmap Example")

plt.show()


This code generates a plot as shown below.


Customizing Plots in Matplotlib and Seaborn


Both libraries allow extensive customization to enhance the readability of visualizations.


Customizing Titles, Labels, and Legends

plt.plot(x, y, label="Sine Wave", color="red")

plt.xlabel("Time")

plt.ylabel("Amplitude")

plt.title("Customized Plot Example")

plt.legend()

plt.grid(True)

plt.show()

This code generates a plot as shown below.


Changing Seaborn Themes

Seaborn provides built-in themes for different aesthetics:

sns.set_style("whitegrid")  # Options: darkgrid, whitegrid, dark, white, ticks

sns.lineplot(x=x, y=y)

plt.title("Seaborn Theme Example")

plt.show()

This code generates a plot as shown below.


Choosing Between Matplotlib and Seaborn

Feature

Matplotlib

Seaborn

Customization

High

Moderate

Ease of Use

Requires more code

Simpler syntax

Plot Aesthetics

Basic

Advanced

Statistical Features

Limited

Extensive

If you need full control and customization, Matplotlib is the better choice. If you prefer statistical plots with minimal coding, Seaborn is more convenient.


Conclusion


Two excellent Python utilities for data visualization are Matplotlib and Seaborn. While Seaborn simplifies statistical visualizations with stunning aesthetics, Matplotlib offers total control over plots. You can find hidden insights in your data by combining the two libraries to create visually stunning and educational visualizations.


📌 Are you prepared to become an expert in data visualization? Improve your data analysis abilities by learning Seaborn, Matplotlib, and other tools!


Enroll in our Data Analysis & Visualization Course right now!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Comentários


bottom of page