Seaborn figure-level vs axes-level in Matplotlib - Performance Comparison
When using Seaborn with matplotlib, it is important to understand how the time to create plots grows as data size increases.
We want to see how figure-level and axes-level plotting functions differ in their time cost as data grows.
Analyze the time complexity of these two Seaborn plotting approaches.
import seaborn as sns
import matplotlib.pyplot as plt
# Figure-level plot
sns.relplot(data=large_data, x="x", y="y", kind="scatter")
# Axes-level plot
fig, ax = plt.subplots()
sns.scatterplot(data=large_data, x="x", y="y", ax=ax)
plt.show()
The first creates a figure-level plot managing figure and axes internally. The second creates an axes-level plot on an existing axes.
Look at what repeats as data size grows.
- Primary operation: Plotting each data point as a marker on the axes.
- How many times: Once per data point in the dataset.
As the number of data points increases, the number of markers to draw grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 markers drawn |
| 100 | 100 markers drawn |
| 1000 | 1000 markers drawn |
Pattern observation: The work grows directly with the number of points; doubling points doubles work.
Time Complexity: O(n)
This means the time to create the plot grows linearly with the number of data points.
[X] Wrong: "Figure-level plots are always slower because they manage more things."
[OK] Correct: Both figure-level and axes-level plots spend most time drawing points, so their time grows similarly with data size.
Understanding how plotting time grows helps you explain performance in data visualization tasks clearly and confidently.
What if we added a loop to create multiple subplots each with n data points? How would the time complexity change?