Bird
0
0

What will be printed when the following code runs and you click inside the plot area?

medium📝 Predict Output Q4 of 15
Matplotlib - Interactive Features
What will be printed when the following code runs and you click inside the plot area?
import matplotlib.pyplot as plt
from matplotlib.widgets import Cursor

fig, ax = plt.subplots()
cursor = Cursor(ax, useblit=True, color='blue', linewidth=1)

def on_click(event):
    print(f'Clicked at x={event.xdata}, y={event.ydata}')

fig.canvas.mpl_connect('button_press_event', on_click)
plt.show()
AAn error because Cursor and event cannot be used together
BNo output will be printed
CClicked at x=<x-coordinate>, y=<y-coordinate> where coordinates are mouse click positions
DClicked at x=None, y=None always
Step-by-Step Solution
Solution:
  1. Step 1: Understand event connection and callback

    The mpl_connect binds mouse button press events to the on_click function, which prints the x and y data coordinates of the click.
  2. Step 2: Confirm Cursor does not block event handling

    The Cursor widget only shows a crosshair and does not interfere with event callbacks, so the print statement executes with actual coordinates.
  3. Final Answer:

    Clicked at x=<x-coordinate>, y=<y-coordinate> where coordinates are mouse click positions -> Option C
  4. Quick Check:

    Event callback prints click coordinates [OK]
Quick Trick: Event callbacks print mouse data coordinates on click [OK]
Common Mistakes:
  • Assuming Cursor blocks event callbacks
  • Expecting no output on click
  • Thinking coordinates are always None

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Matplotlib Quizzes