Dual y-axis for different scales in Matplotlib - Time & Space Complexity
When we create a plot with two y-axes in matplotlib, we want to understand how the time to draw the plot changes as we add more data points.
We ask: How does the plotting time grow when the data size grows?
Analyze the time complexity of the following matplotlib code snippet.
import matplotlib.pyplot as plt
n = 10 # Example value for n
x = range(n)
y1 = [i for i in x]
y2 = [i**2 for i in x]
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax1.plot(x, y1, 'g-')
ax2.plot(x, y2, 'b-')
plt.show()
This code plots two lines sharing the same x-axis but with different y-axes scales.
- Primary operation: Plotting each data point for both y1 and y2 lines.
- How many times: Each line plots n points, so 2 * n plotting operations.
As the number of data points n increases, the number of plotting operations grows roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 20 (2 lines * 10 points) |
| 100 | 200 (2 lines * 100 points) |
| 1000 | 2000 (2 lines * 1000 points) |
Pattern observation: The operations grow linearly with the number of points.
Time Complexity: O(n)
This means the time to plot grows directly in proportion to the number of data points.
[X] Wrong: "Adding a second y-axis doubles the time complexity to O(n^2)."
[OK] Correct: The second y-axis adds another line to plot, but plotting each point is still done once per point, so the growth stays linear, not squared.
Understanding how plotting time grows helps you explain performance when visualizing large datasets, a useful skill in data science and analysis.
What if we added a third y-axis with another line? How would the time complexity change?
Practice
Solution
Step 1: Understand the purpose of dual y-axis
A dual y-axis allows plotting two datasets with different value ranges on the same graph for easy comparison.Step 2: Identify the correct use case
Comparing datasets with different scales requires two y-axes; other options do not describe this purpose.Final Answer:
To compare two datasets with different scales on the same plot -> Option AQuick Check:
Dual y-axis = compare different scales [OK]
- Thinking dual y-axis creates two separate plots
- Confusing dual y-axis with adding legends
- Assuming it changes line colors only
Solution
Step 1: Recall matplotlib methods for axes
The methodtwinx()creates a second y-axis sharing the x-axis.Step 2: Differentiate from similar methods
twiny()creates a second x-axis, not y-axis;subplots()creates figure and axes;plot()draws lines.Final Answer:
ax.twinx() -> Option CQuick Check:
Second y-axis = twinx() [OK]
- Using plt.subplots() to create second y-axis
- Confusing twinx() with twiny()
- Trying to use plt.plot() to add axes
import matplotlib.pyplot as plt fig, ax1 = plt.subplots() ax2 = ax1.twinx() ax1.plot([1, 2, 3], [10, 20, 30], 'g-') ax2.plot([1, 2, 3], [100, 200, 300], 'b-') print(len(fig.axes))
Solution
Step 1: Understand axes creation
plt.subplots()creates one axes (ax1), thenax1.twinx()adds a second y-axis (ax2), so total axes are 2.Step 2: Check figure axes count
fig.axesis a list of all axes in the figure; after addingax2, length is 2.Final Answer:
2 -> Option AQuick Check:
Two axes after twinx() = 2 [OK]
- Assuming twinx() replaces the original axes
- Thinking twinx() adds multiple axes
- Confusing axes count with number of plots
import matplotlib.pyplot as plt fig, ax = plt.subplots() ax2 = plt.twinx() ax.plot([1,2,3], [10,20,30]) ax2.plot([1,2,3], [100,200,300]) plt.show()
Solution
Step 1: Check method usage for second y-axis
twinx()is a method of an axes object, not plt module. Soplt.twinx()is invalid.Step 2: Correct method call
Useax.twinx()to create the second y-axis sharing the x-axis.Final Answer:
plt.twinx() should be ax.twinx() -> Option DQuick Check:
twinx() is an axes method, not plt function [OK]
- Calling twinx() on plt instead of axes
- Confusing figure and axes creation methods
- Missing plt.show() (though present here)
Solution
Step 1: Assign correct y-axis labels
Temperature and rainfall are y-axis data, so useset_ylabel()on each axis with matching labels.Step 2: Use colors to distinguish axes
Setting label colors helps visually separate the two y-axes for clarity.Final Answer:
ax1.set_ylabel('Temperature (°C)', color='red')\nax2.set_ylabel('Rainfall (mm)', color='blue') -> Option BQuick Check:
Correct y-labels and colors = ax1.set_ylabel('Temperature (°C)', color='red')\nax2.set_ylabel('Rainfall (mm)', color='blue') [OK]
- Setting x-axis labels instead of y-axis
- Swapping labels between axes
- Using titles instead of axis labels
Ready to prove it?
Pass the test and this pattern moves unknown
