0
0
Matplotlibdata~20 mins

Inverted axes in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Inverted Axes Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this inverted x-axis plot code?
Consider the following Python code using matplotlib. What will be the x-axis limits after running this code?
Matplotlib
import matplotlib.pyplot as plt
plt.plot([1, 2, 3, 4], [10, 20, 25, 30])
plt.gca().invert_xaxis()
limits = plt.gca().get_xlim()
print(limits)
Aprint outputs (4.0, 1.0)
Bprint outputs (1.0, 4.0)
Cprint outputs (10.0, 30.0)
Dprint outputs (30.0, 10.0)
Attempts:
2 left
💡 Hint
Remember that invert_xaxis() reverses the x-axis limits.
data_output
intermediate
2:00remaining
What is the y-axis range after inversion?
Given this code snippet, what will be the y-axis limits printed?
Matplotlib
import matplotlib.pyplot as plt
plt.plot([0, 1, 2], [5, 10, 15])
plt.gca().invert_yaxis()
limits = plt.gca().get_ylim()
print(limits)
Aprint outputs (5.0, 15.0)
Bprint outputs (15.0, 5.0)
Cprint outputs (0.0, 2.0)
Dprint outputs (2.0, 0.0)
Attempts:
2 left
💡 Hint
invert_yaxis() reverses the y-axis limits.
visualization
advanced
3:00remaining
Which plot shows the x-axis inverted correctly?
You have four plots showing the same data but with different axis settings. Which plot correctly inverts the x-axis?
Matplotlib
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(5)
y = x**2
fig, axs = plt.subplots(2, 2)

# Plot A
axs[0,0].plot(x, y)
axs[0,0].set_title('A')

# Plot B
axs[0,1].plot(x, y)
axs[0,1].invert_xaxis()
axs[0,1].set_title('B')

# Plot C
axs[1,0].plot(x, y)
axs[1,0].invert_yaxis()
axs[1,0].set_title('C')

# Plot D
axs[1,1].plot(x, y)
axs[1,1].invert_xaxis()
axs[1,1].invert_yaxis()
axs[1,1].set_title('D')

plt.tight_layout()
plt.show()
APlot D
BPlot A
CPlot C
DPlot B
Attempts:
2 left
💡 Hint
invert_xaxis() flips the x-axis direction.
🧠 Conceptual
advanced
2:00remaining
What happens if you invert both axes twice?
If you call invert_xaxis() and invert_yaxis() twice each on the same plot, what will be the final axis directions?
ABoth axes will be inverted once, so both axes reversed
BOnly x-axis will be inverted, y-axis normal
CBoth axes will return to normal direction (not inverted)
DOnly y-axis will be inverted, x-axis normal
Attempts:
2 left
💡 Hint
Inverting an axis twice cancels the inversion.
🔧 Debug
expert
3:00remaining
Why does this code not invert the x-axis as expected?
This code attempts to invert the x-axis but the plot shows normal x-axis direction. What is the reason?
Matplotlib
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])
plt.invert_xaxis()
plt.show()
Aplt.invert_xaxis() is not a valid function; invert_xaxis() must be called on the Axes object
BThe plot data is too small to show inversion
Cinvert_xaxis() only works after plt.show()
DThe x-axis is inverted but the y-axis is overriding it
Attempts:
2 left
💡 Hint
Check where invert_xaxis() is called from.