0
0
Data Analysis Pythondata~5 mins

Figure and axes creation in Data Analysis Python - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Figure and axes creation
O(n*m)
Understanding Time Complexity

When we create figures and axes in data analysis, we want to know how the time it takes changes as we add more elements.

We ask: How does the work grow when we create more axes or figures?

Scenario Under Consideration

Analyze the time complexity of the following code snippet.

import matplotlib.pyplot as plt

fig, ax = plt.subplots(nrows=3, ncols=2)

for i in range(3):
    for j in range(2):
        ax[i, j].plot([1, 2, 3], [1, 4, 9])

This code creates a figure with 6 axes arranged in 3 rows and 2 columns, then plots a simple line on each axis.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: Nested loops over rows and columns to plot on each axis.
  • How many times: The inner plot command runs once for each axis, total 3 x 2 = 6 times.
How Execution Grows With Input

As the number of rows and columns increases, the total axes grow by multiplying these two numbers.

Input Size (rows x cols)Approx. Operations
3 x 2 = 66 plots
10 x 10 = 100100 plots
30 x 30 = 900900 plots

Pattern observation: The work grows proportionally to the total number of axes created.

Final Time Complexity

Time Complexity: O(n*m)

This means the time to create and plot on axes grows linearly with the number of axes (rows times columns).

Common Mistake

[X] Wrong: "Creating more axes takes the same time no matter how many there are."

[OK] Correct: Each axis requires separate setup and plotting, so more axes mean more work and more time.

Interview Connect

Understanding how plotting scales helps you manage performance when working with many charts or subplots in real projects.

Self-Check

"What if we added a loop inside each axis to plot multiple lines? How would the time complexity change?"