Plot customization (title, labels, figsize) in Pandas - Time & Space Complexity
We want to see how changing plot settings affects the time it takes to create a plot.
How does adding titles, labels, or changing size change the work done?
Analyze the time complexity of the following code snippet.
import pandas as pd
import matplotlib.pyplot as plt
data = pd.Series(range(1000))
ax = data.plot(figsize=(8, 4))
ax.set_title('Sample Plot')
ax.set_xlabel('Index')
ax.set_ylabel('Value')
plt.close()
This code creates a line plot from 1000 points and adds a title and axis labels with a set figure size.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Plotting the 1000 data points (drawing each point).
- How many times: Once for each data point (1000 times).
Adding titles, labels, and figure size settings takes about the same time no matter the data size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | ~10 drawing operations + fixed title/label setup |
| 100 | ~100 drawing operations + fixed title/label setup |
| 1000 | ~1000 drawing operations + fixed title/label setup |
Pattern observation: The main work grows with the number of points, but title and label setup stays constant.
Time Complexity: O(n)
This means the time grows linearly with the number of data points, while adding titles and labels does not add extra growth.
[X] Wrong: "Adding a title or labels makes the plot take much longer as data grows."
[OK] Correct: Titles and labels are set once and do not depend on data size, so they add only a small fixed cost.
Understanding which parts of code grow with data size helps you explain your choices clearly and confidently.
"What if we added a loop to create multiple plots each with titles and labels? How would the time complexity change?"