0
0
Pandasdata~5 mins

Subplots for multiple charts in Pandas - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Subplots for multiple charts
O(n)
Understanding Time Complexity

When we create multiple charts using subplots in pandas, we want to know how the time to draw them changes as we add more charts.

We ask: How does the work grow when we increase the number of charts?

Scenario Under Consideration

Analyze the time complexity of the following code snippet.


import pandas as pd
import matplotlib.pyplot as plt

data = pd.DataFrame({
    'A': range(100),
    'B': range(100, 200),
    'C': range(200, 300)
})

fig, axes = plt.subplots(3, 1)
for i, col in enumerate(data.columns):
    data[col].plot(ax=axes[i])
plt.show()
    

This code creates three line charts stacked vertically, one for each column in the data.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: Looping over each column to draw a chart.
  • How many times: Once for each column (number of charts).
How Execution Grows With Input

Each new chart adds roughly the same amount of work to draw it.

Input Size (n)Approx. Operations
3 charts3 times the work of one chart
10 charts10 times the work of one chart
100 charts100 times the work of one chart

Pattern observation: The work grows directly in proportion to the number of charts.

Final Time Complexity

Time Complexity: O(n)

This means if you double the number of charts, the time to draw them roughly doubles.

Common Mistake

[X] Wrong: "Drawing multiple charts together is as fast as drawing just one chart."

[OK] Correct: Each chart requires its own drawing steps, so more charts mean more work and more time.

Interview Connect

Understanding how drawing multiple charts scales helps you explain performance in data visualization tasks clearly and confidently.

Self-Check

What if we changed the code to plot all columns on the same single chart instead of separate subplots? How would the time complexity change?