Python Matplotlib Seaborn Explained Simply (with Diagrams and Real Code)
Python Matplotlib Seaborn: The Essentials in One Article — Real Code, Diagrams and Concrete Steps, Excerpts from a 37-Lesson Course.
A no-nonsense guide: Python Matplotlib Seaborn broken down with diagrams, concrete examples and tested commands. Everything comes from a structured 11-chapter course — here are the highlights.
- Introduction and Installation
- Matplotlib Basics
- Essential Matplotlib Charts
- Customization and Styles
- Subplots and Complex Figures
Exploration and Initial Visualizations (EDA)
Why perform EDA before building the dashboard?
In this lesson we will create 5 exploratory charts to understand:
Common setup
import pandas as pd import matplotlib.pyplot as plt import seaborn as sns sns.set_theme( style="whitegrid", context="notebook", palette="viridis", font_scale=1.05, ) df = pd.read_csv("ventes_2024.csv", parse_dates=["date"]) print(df.info()) print(df.describe())
Chart 1: Average basket distribution
fig, ax = plt.subplots(figsize=(10, 5)) sns.histplot(data=df, x="vente_eur", kde=True, bins=40, color="#7c3aed", ax=ax) mean_vente = df["vente_eur"].mean() median_vente = df["vente_eur"].median() ax.axvline(mean_vente, color="red", linestyle="--", linewidth=2, label=f"Mean: {mean_vente:.0f} EUR") ax.axvline(median_vente, color="orange", linestyle="--", linewidth=2, label=f"Median: {median_vente:.0f} EUR") ax.set_title("Average basket distribution", fontweight="bold") ax.set_xlabel("Order amount (EUR)") ax.set_ylabel("Number of transactions") ax.legend() sns.despine() plt.tight_layout() plt.show()
Chart 2: Monthly revenue trend
monthly = df.groupby("mois").agg( ca=("vente_eur", "sum"), nb_cmd=("vente_eur", "count"), ).reset_index() fig, ax = plt.subplots(figsize=(11, 5)) sns.lineplot(data=monthly, x="mois", y="ca", marker="o", linewidth=2.5, color="#7c3aed", ax=ax) ax.fill_between(monthly["mois"], monthly["ca"], alpha=0.15, color="#7c3aed") best_month = monthly.loc[monthly["ca"].idxmax()] ax.annotate( f"Peak: {best_month['ca']:,.0f} EUR", xy=(best_month["mois"], best_month["ca"]), xytext=(best_month["mois"], best_month["ca"] + 1500), ha="center", fontsize=11, fontweight="bold", color="red", arrowprops=dict(arrowstyle="->", color="red") ) ax.set_title("Monthly revenue evolution 2024", fontweight="bold") ax.set_xlabel("Month"); ax.set_ylabel("Revenue (EUR)") ax.set_xticks(range(1, 13)) sns.despine() plt.tight_layout() plt.show()
Chart 3: Performance by category
cat_perf = df.groupby("categorie").agg( ca=("vente_eur", "sum"), marge=("marge_eur", "sum"), ).reset_index().sort_values("ca", ascending=True) fig, ax = plt.subplots(figsize=(10, 5)) y_pos = range(len(cat_perf)) ax.barh(y_pos, cat_perf["ca"], color="#a78bfa", label="Revenue", alpha=0.8) ax.barh(y_pos, cat_perf["marge"], color="#7c3aed", label="Margin", alpha=0.9) for i, (ca, marge) in enumerate(zip(cat_perf["ca"], cat_perf["marge"])): ax.text(ca + 2000, i, f"{ca:,.0f} EUR", va="center", fontsize=10, fontweight="bold") ax.set_yticks(y_pos) ax.set_yticklabels(cat_perf["categorie"]) ax.set_title("Revenue and margins by product category", fontweight="bold") ax.set_xlabel("Amount (EUR)") ax.legend(loc="lower right") sns.despine() plt.tight_layout() plt.show()
Chart 4: Store performance (boxplot)
fig, ax = plt.subplots(figsize=(11, 5)) order = df.groupby("magasin")["vente_eur"].median().sort_values(ascending=False).index sns.boxplot(data=df, x="magasin", y="vente_eur", order=order, palette="viridis", hue="magasin", legend=False, ax=ax) sns.stripplot(data=df, x="magasin", y="vente_eur", order=order, color="black", alpha=0.15, size=2, ax=ax) ax.set_title("Basket distribution by store (sorted by median)", fontweight="bold") ax.set_xlabel("Store"); ax.set_ylabel("Basket (EUR)") sns.despine() plt.tight_layout() plt.show()
Chart 5: Numeric correlations (heatmap)
numeric_cols = ["vente_eur", "marge_eur", "nb_articles", "mois", "trimestre"] corr = df[numeric_cols].corr() fig, ax = plt.subplots(figsize=(8, 6)) sns.heatmap(corr, annot=True, fmt=".2f", cmap="vlag", center=0, square=True, linewidths=0.5, cbar_kws={"shrink": 0.7}, ax=ax) ax.set_title("Correlation matrix of numeric variables", fontweight="bold") plt.tight_layout() plt.show()
First visualization script
Learning objectives
import numpy as np and import matplotlib.pyplot as plt.The complete script: 8 lines for your first chart
Here is the script we will dissect together. Copy it into a new Jupyter notebook:
import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 2 * np.pi, 100) y = np.sin(x) plt.plot(x, y) plt.title("Ma premiere fonction sinus") plt.xlabel("x (radians)") plt.ylabel("sin(x)") plt.grid(True) plt.show()
Run the cell (Shift + Enter). You should see a beautiful sinusoidal curve oscillating between −1 and +1.
Line-by-line breakdown
Lines 1–2: the imports
import numpy as np import matplotlib.pyplot as plt
Two universal conventions:
import matplotlib.pyplot as plt and never import matplotlib as plt. The pyplot module contains all the plot(), title(), etc. functions.Line 3: generate the x-axis
x = np.linspace(0, 2 * np.pi, 100)
np.linspace(start, stop, n) returns an array of n evenly spaced values between start and stop. Here: 100 points between 0 and 2π (≈ 6.28).
Result: x = [0.0, 0.063, 0.127, 0.190, ..., 6.283]
Line 4: compute y = sin(x)
y = np.sin(x)
NumPy applies sin() to every element of the array x in a single operation. This is NumPy’s vectorized magic: no loops, extremely fast.
Result: y = [0.0, 0.063, 0.127, ..., -0.0]
Line 5: plot the curve
plt.plot(x, y)
plt.plot(x, y) draws a line connecting each point (x[i], y[i]). This is the most-used function in all of Matplotlib.
Lines 6–8: title, labels and grid
plt.title("Ma premiere fonction sinus") plt.xlabel("x (radians)") plt.ylabel("sin(x)") plt.grid(True)
First line chart with real data
Learning objectives
Our dataset: fictional monthly sales
For this exercise we create a small DataFrame with 12 months of sales for 3 different products:
import pandas as pd import matplotlib.pyplot as plt data = { "Mois": ["Jan", "Fev", "Mar", "Avr", "Mai", "Juin", "Juil", "Aout", "Sep", "Oct", "Nov", "Dec"], "Produit A": [120, 135, 148, 160, 175, 210, 250, 245, 200, 170, 140, 290], "Produit B": [80, 85, 90, 95, 100, 110, 130, 135, 120, 100, 90, 180], "Produit C": [50, 55, 60, 70, 85, 100, 120, 115, 95, 75, 60, 150], } df = pd.DataFrame(data) print(df.head())
Displayed result:
Mois Produit A Produit B Produit C 0 Jan 120 80 50 1 Fev 135 85 55 2 Mar 148 90 60 3 Avr 160 95 70 4 Mai 175 100 85
Plot a single column
fig, ax = plt.subplots(figsize=(10, 5)) ax.plot(df["Mois"], df["Produit A"], color="purple", linewidth=2) ax.set_title("Ventes mensuelles du Produit A", fontsize=14) ax.set_xlabel("Mois") ax.set_ylabel("Unites vendues") ax.grid(True, alpha=0.3) plt.tight_layout() plt.show()
You obtain a clean chart showing sales seasonality (peak in July, surge in December).
Plot multiple series on the same chart
fig, ax = plt.subplots(figsize=(12, 6)) ax.plot(df["Mois"], df["Produit A"], label="Produit A", linewidth=2, marker="o") ax.plot(df["Mois"], df["Produit B"], label="Produit B", linewidth=2, marker="s") ax.plot(df["Mois"], df["Produit C"], label="Produit C", linewidth=2, marker="^") ax.set_title("Ventes mensuelles par produit", fontsize=14, pad=15) ax.set_xlabel("Mois", fontsize=12) ax.set_ylabel("Unites vendues", fontsize=12) ax.legend(loc="upper left", fontsize=11) ax.grid(True, alpha=0.3) plt.tight_layout() plt.show()
Three new techniques applied:
Available markers
| Code | Marker | Code | Marker |
|---|---|---|---|
"o" | Circle | "s" | Square |
"^" | Triangle up | "v" | Triangle down |
"<" | Triangle left | ">" | Triangle right |
"D" | Diamond | "d" | Thin diamond |
"*" | Star | "+" | Plus |
"x" | Cross | "." | Point |
"P" | Filled plus | "X" | Filled cross |
Load a real CSV file
In real life your data lives in a .csv file. Here is how to load it:
# If the file is in the same folder df = pd.read_csv("ventes.csv") # If the file is on the web url = "https://raw.githubusercontent.com/exemple/data/main/ventes.csv" df = pd.read_csv(url) # With a different separator (semicolon) df = pd.read_csv("ventes.csv", sep=";") # With automatic date parsing df = pd.read_csv("ventes.csv", parse_dates=["date"]) print(df.head()) print(df.dtypes)
df.dtypes after loading. If a column expected to be numeric appears as object, you probably have French decimal commas or empty cells to clean.Pandas + Matplotlib shortcut: built-in wrapper
Pandas ships with its own Matplotlib wrapper. You can plot directly from a DataFrame:
df.set_index("Mois").plot(figsize=(10, 5), marker="o") plt.title("Ventes par produit (style Pandas)") plt.ylabel("Unites") plt.grid(True, alpha=0.3) plt.show()
This article covers the most useful excerpts — the complete Python Matplotlib Seaborn course (11 chapters, 37 lessons, corrected exercises and final project) takes you all the way.
./acceder-au-cours-complet free course: Mastering Claude CodeFAQ
How long does it take to learn Python Matplotlib Seaborn?
Are there any prerequisites?
Where should I start concretely?
📬 Want to receive this kind of guide every week? Subscribe for free — real code, zero fluff.