Skip to course content
Free data visualization course

Data Visualization and Dashboard Storytelling

Unit 10.04: Handing a Seaborn figure back to Matplotlib

Seaborn returns Matplotlib objects, so nothing from Module 9 stops applying.

Statistical work first, control second

A Seaborn chart, then axis limits, a finding-carrying title, an annotation and a spine removal applied to it.

The code shows the returned object is an ordinary axes.

import matplotlib
matplotlib.use("Agg")
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

df = pd.DataFrame({"month": list(range(1, 13)),
                   "revenue": [100 + m * 3 for m in range(1, 13)]})

ax = sns.lineplot(data=df, x="month", y="revenue")
ax.set_ylim(0, 160)
ax.set_title("Revenue grew 33% over the year", loc="left", fontweight="bold")
ax.annotate("new pricing", xy=(7, 121), xytext=(3, 140),
            arrowprops={"arrowstyle": "->"})
ax.spines["top"].set_visible(False)

print(f"axes object      : {type(ax).__name__}   <- an ordinary Matplotlib Axes")
print(f"y-limits set     : {ax.get_ylim()}")
print(f"annotations      : {len(ax.texts)}")
print(f"title            : {ax.get_title(loc='left')!r}")
plt.close("all")

print("\nSeaborn returns Matplotlib objects, so everything from Module 9 still")
print("applies. Use Seaborn for the statistical work, then take control back.")

Every technique from Module 9 works on it: limits, annotation in data coordinates, house style, saving. The library boundary is not a wall.

This is the pattern worth adopting. Explore with Seaborn, where the statistical summaries and faceting are one line each, then take the axes and finish the deliverable with the control Matplotlib gives you.

The mistake this prevents

The mistake is rebuilding a Seaborn chart in raw Matplotlib to change one thing. The object is already a Matplotlib axes - change the thing.

Takeaway

Explore in Seaborn, finish in Matplotlib. The returned object is an ordinary axes and every Module 9 technique applies to it.