Adding and Customizing Legends in Pandas & Seaborn Plots – Best Practices 2026
A well-designed legend is essential for making your plots clear and professional. In 2026, properly customizing legends in Pandas and Seaborn helps viewers quickly understand what each line, bar, or marker represents, especially when layering multiple series or using the hue parameter.
TL;DR — Key Legend Techniques
- Use
label=when plotting multiple series - Use
plt.legend()to control position, title, and style - Use
huein Seaborn for automatic legends - Place legends outside the plot when space is limited
1. Basic Legend with Pandas Plot
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv("sales_data.csv", parse_dates=["order_date"])
monthly = df.resample("M", on="order_date").agg({"amount": "sum", "quantity": "sum"}).reset_index()
plt.figure(figsize=(11, 6))
plt.plot(monthly["order_date"], monthly["amount"], label="Total Sales", linewidth=2.5)
plt.plot(monthly["order_date"], monthly["quantity"], label="Total Quantity", linewidth=2.5)
plt.title("Monthly Sales and Quantity Trend")
plt.ylabel("Value")
plt.xlabel("Date")
plt.legend(title="Metrics", fontsize=10, title_fontsize=11)
plt.grid(True, alpha=0.3)
plt.show()
2. Professional Legend with Seaborn
import seaborn as sns
plt.figure(figsize=(12, 7))
sns.lineplot(
data=df,
x="order_date",
y="amount",
hue="region",
linewidth=2.2,
marker="o",
markersize=5
)
plt.title("Sales Trends by Region")
plt.ylabel("Sales Amount")
plt.xlabel("Date")
# Customizing the legend
plt.legend(
title="Region",
title_fontsize=12,
fontsize=10,
loc="upper left",
bbox_to_anchor=(1.02, 1), # Place legend outside the plot
frameon=True
)
plt.tight_layout()
plt.show()
3. Advanced Legend Customization
plt.figure(figsize=(10, 6))
sns.scatterplot(
data=df,
x="quantity",
y="amount",
hue="region",
size="profit",
alpha=0.7
)
plt.title("Quantity vs Amount by Region")
plt.legend(
title="Region & Profit",
bbox_to_anchor=(1.05, 1),
loc='upper left',
ncol=1, # Single column legend
frameon=True,
shadow=True
)
plt.tight_layout()
plt.show()
4. Best Practices for Legends in 2026
- Always add a meaningful
titleto your legend - Use
bbox_to_anchor=(1.02, 1)to place legends outside the plot when space is tight - Use
fontsizeandtitle_fontsizeto control readability - Keep the number of legend items reasonable (ideally ≤ 8)
- Use
plt.tight_layout()after adjusting legend position - For Seaborn plots with
hue, customize the legend after plotting
Conclusion
A good legend can make or break the clarity of your visualization. In 2026, spending a little extra time customizing legends with proper titles, positioning, and styling significantly improves the professionalism and readability of your plots. Whether using Pandas .plot() or Seaborn, always ensure your legend clearly communicates what each element represents.
Next steps:
- Review your existing plots and improve their legends by adding titles, adjusting position, and using
bbox_to_anchorwhen needed