Bird
0
0

What will be the output of this code snippet?

medium📝 Predict Output Q13 of 15
Matplotlib - Real-World Visualization Patterns
What will be the output of this code snippet?
import matplotlib.pyplot as plt
values = [50, 20, 30]
labels = ['A', 'B', 'C']
plt.barh(labels, values)
plt.gca().invert_yaxis()
plt.show()
AA horizontal bar chart with 'C' at the top and 'A' at the bottom
BA vertical bar chart with bars labeled A, B, C
CA horizontal bar chart with 'A' at the top and 'C' at the bottom
DAn error because invert_yaxis() is invalid here
Step-by-Step Solution
Solution:
  1. Step 1: Understand horizontal bar chart with invert_yaxis()

    The barh function plots horizontal bars with labels on y-axis. By default, y-axis starts from bottom.
  2. Step 2: Effect of invert_yaxis()

    Calling invert_yaxis() flips the y-axis so the first label 'A' appears at the top, making ranking easier to read.
  3. Final Answer:

    A horizontal bar chart with 'A' at the top and 'C' at the bottom -> Option C
  4. Quick Check:

    invert_yaxis flips labels top-down [OK]
Quick Trick: invert_yaxis() flips bars so top label is first [OK]
Common Mistakes:
  • Thinking invert_yaxis() causes error
  • Confusing horizontal with vertical bars
  • Assuming labels order stays bottom-up

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Matplotlib Quizzes