Python Mastery – From Basics to Brilliance / Python Libraries for Real-World Use

Matplotlib / Seaborn – Plotting & Visualization

πŸ“Š Matplotlib & Seaborn

Both Matplotlib and Seaborn are Python libraries used for data visualization β€” creating charts and graphs from data.


πŸ“ˆ Matplotlib

Matplotlib is a general-purpose plotting library that gives you detailed control over your charts.

πŸ“¦ Install

pip install matplotlib

Import it:

import matplotlib.pyplot as plt

πŸ“Š Basic Example

import matplotlib.pyplot as plt

x = [1, 2, 3, 4]
y = [10, 20, 15, 25]

plt.plot(x, y)

plt.xlabel("Days")
plt.ylabel("Sales")
plt.title("Sales Trend")

plt.show() #It creates a line chart.

Common Charts

plt.plot()         # Line chart
plt.bar()          # Bar chart
plt.scatter()      # Scatter plot
plt.hist()         # Histogram
plt.pie()          # Pie chart
plt.boxplot()      # Box plot
plt.fill_between() # Area chart
plt.stackplot()    # Stacked area chart

🎨 Seaborn

Seaborn is built on top of Matplotlib and makes it easier to create attractive and statistical visualizations.

πŸ“¦ Install

pip install seaborn

Import it:

import seaborn as sns

πŸ“Š Example

import seaborn as sns
import matplotlib.pyplot as plt

data = [10, 20, 20, 30, 40, 40, 50]

sns.histplot(data)
plt.show()

Seaborn is particularly useful for statistical plots and working with Pandas DataFrames.

Common Charts

sns.lineplot()       # Line chart
sns.barplot()        # Bar chart
sns.countplot()      # Count chart
sns.scatterplot()    # Scatter plot
sns.histplot()       # Histogram
sns.boxplot()        # Box plot
sns.violinplot()     # Violin plot
sns.heatmap()        # Heatmap
sns.pairplot()       # Pair plot
sns.regplot()        # Regression plot

βš–οΈ Matplotlib vs Seaborn

Matplotlib πŸ“ˆ

Seaborn 🎨

General-purpose visualization

Statistical visualization

More control over individual elements

Easier to create complex plots

More code for some plots

Often requires less code

Works independently

Built on Matplotlib

plt.plot()

sns.histplot()