Dates on x-axis in Matplotlib - Time & Space Complexity
When plotting dates on the x-axis using matplotlib, it's important to understand how the plotting time changes as the number of dates grows.
We want to know how the time to draw the plot increases when we add more date points.
Analyze the time complexity of the following code snippet.
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import datetime
dates = [datetime.date(2023, 1, i+1) for i in range(100)]
values = list(range(100))
plt.figure()
plt.plot(dates, values)
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
plt.gcf().autofmt_xdate()
plt.show()
This code plots 100 dates on the x-axis with corresponding values, formatting the dates for better readability.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Plotting each date-value pair on the graph.
- How many times: Once for each date in the list (n times).
As the number of dates increases, the plotting time grows roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 plot points processed |
| 100 | 100 plot points processed |
| 1000 | 1000 plot points processed |
Pattern observation: Doubling the number of dates roughly doubles the work done to plot them.
Time Complexity: O(n)
This means the time to plot grows linearly with the number of dates you want to show.
[X] Wrong: "Plotting dates is always slow because date formatting is complex and slows everything down."
[OK] Correct: While formatting dates adds some work, the main time cost comes from plotting each point, which grows linearly. The formatting cost is small compared to plotting many points.
Understanding how plotting time grows with data size helps you explain performance in data visualization tasks clearly and confidently.
What if we changed the x-axis to show only monthly ticks instead of daily dates? How would the time complexity change?