Date formatting with mdates in Matplotlib - Time & Space Complexity
We want to understand how the time to format dates grows when using matplotlib's mdates.
How does the work change as we add more dates to format?
Analyze the time complexity of the following code snippet.
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import datetime
# Create a list of dates
dates = [datetime.date(2024, 1, 1) + datetime.timedelta(days=i) for i in range(100)]
fig, ax = plt.subplots()
ax.plot(dates, range(100))
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
plt.show()
This code plots 100 points with dates on the x-axis and formats those dates using mdates.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Formatting each date label on the x-axis.
- How many times: Once for each date tick shown (up to the number of dates).
As the number of dates increases, the number of formatting operations grows roughly the same.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 date formats |
| 100 | About 100 date formats |
| 1000 | About 1000 date formats |
Pattern observation: The work grows linearly as we add more dates to format.
Time Complexity: O(n)
This means the time to format dates grows directly in proportion to the number of dates.
[X] Wrong: "Formatting dates happens instantly no matter how many there are."
[OK] Correct: Each date label needs to be processed, so more dates mean more work and more time.
Understanding how formatting scales helps you write efficient plots and handle large datasets smoothly.
"What if we used fewer date ticks by changing the locator? How would the time complexity change?"