Line chart with multiple series
Several lines on shared axes show how groups move relative to each other. With a long-format table (one row per group and x value), a groupby loop draws one line per group.
When to use it
- A handful of groups share the same x-axis and units.
- You want to see crossings, divergence or common seasonality.
Watch out: Past five or six lines the chart turns into spaghetti. Use small multiples instead.
Ask for it in pltchat
Upload your file and type something like:
"one line per region, x = year, y = sales, legend outside on the right"
pltchat writes the loader and the plot for your columns, runs it in your browser and shows you the code.
The matplotlib code
Self-contained with example data; swap in pd.read_csv(...) or
pd.read_excel(...) for your own file.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# Example data in long format: one row per (region, year)
rng = np.random.default_rng(1)
years = np.arange(2015, 2025)
df = pd.DataFrame([
{"region": r, "year": y, "sales": base + 3 * i + rng.normal(0, 2)}
for r, base in [("North", 20), ("South", 28), ("East", 15), ("West", 24)]
for i, y in enumerate(years)
])
fig, ax = plt.subplots(figsize=(6.4, 4))
for region, group in df.groupby("region"):
ax.plot(group["year"], group["sales"], marker="o", label=region)
ax.set_xlabel("Year")
ax.set_ylabel("Sales (units)")
ax.legend(title="Region", loc="center left", bbox_to_anchor=(1, 0.5))
ax.grid(alpha=0.3)
plt.tight_layout()
plt.show()
Upload a CSV, Excel, JSON or Parquet file. Your first plot is always free.