0
0
Matplotlibdata~5 mins

Dumbbell charts in Matplotlib - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Dumbbell charts
O(n)
Understanding Time Complexity

We want to understand how the time it takes to draw a dumbbell chart changes as we add more data points.

How does the work grow when the number of points increases?

Scenario Under Consideration

Analyze the time complexity of the following code snippet.

import matplotlib.pyplot as plt

# Data points
n = 100
x1 = range(n)
x2 = [i + 1 for i in x1]
y = range(n)

plt.hlines(y, x1, x2, color='gray')
plt.scatter(x1, y, color='blue')
plt.scatter(x2, y, color='red')
plt.show()

This code draws horizontal lines and two sets of points for each data item to create a dumbbell chart.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: Drawing lines and points for each data item.
  • How many times: Once per data point, repeated n times.
How Execution Grows With Input

Each new data point adds a line and two points to draw, so the work grows steadily with the number of points.

Input Size (n)Approx. Operations
10~30 drawing actions
100~300 drawing actions
1000~3000 drawing actions

Pattern observation: The number of drawing actions grows directly in proportion to the number of data points.

Final Time Complexity

Time Complexity: O(n)

This means the time to draw the chart grows in a straight line as you add more points.

Common Mistake

[X] Wrong: "Adding more points won't affect drawing time much because matplotlib is fast."

[OK] Correct: Each point and line must be drawn separately, so more points mean more work and longer drawing time.

Interview Connect

Understanding how drawing time grows helps you explain performance when working with visualizations in real projects.

Self-Check

"What if we added a loop inside the drawing code to draw multiple lines per data point? How would the time complexity change?"