Bird
0
0

Consider the code below:

medium📝 Predict Output Q13 of 15
Matplotlib - Interactive Features
Consider the code below:
import matplotlib.pyplot as plt

fig, ax = plt.subplots()

def on_move(event):
    print(f"Mouse at: {event.xdata}, {event.ydata}")

cid = fig.canvas.mpl_connect('motion_notify_event', on_move)
plt.show()

What will happen when you move the mouse over the plot area?
AThe coordinates of the mouse pointer inside the plot will be printed continuously
BNothing will happen because the event is not connected properly
CThe plot will close immediately
DAn error will occur because <code>event.xdata</code> is undefined
Step-by-Step Solution
Solution:
  1. Step 1: Understand the event type 'motion_notify_event'

    This event triggers whenever the mouse moves over the figure canvas.
  2. Step 2: Analyze the function on_move

    The function prints the mouse coordinates inside the plot area using event.xdata and event.ydata.
  3. Final Answer:

    The coordinates of the mouse pointer inside the plot will be printed continuously -> Option A
  4. Quick Check:

    Mouse move event prints coords = B [OK]
Quick Trick: motion_notify_event tracks mouse movement over plot [OK]
Common Mistakes:
  • Assuming no output because event is not connected
  • Thinking event.xdata is always None or undefined
  • Expecting plot to close on mouse move

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Matplotlib Quizzes