Bird
0
0

What is wrong with this button widget code?

medium📝 Debug Q7 of 15
Matplotlib - Interactive Features
What is wrong with this button widget code?
import matplotlib.pyplot as plt
from matplotlib.widgets import Button

fig, ax = plt.subplots()
button_ax = plt.axes([0.7, 0.05, 0.1, 0.075])
button = Button(button_ax, 'Press')

button.on_clicked(on_press)

def on_press(event):
    print('Button pressed')
AButton label must be an integer
BThe callback function on_press is defined after it is used
CAxes coordinates are out of range
DButton widget requires a slider parameter
Step-by-Step Solution
Solution:
  1. Step 1: Check callback function order

    The on_press function is passed to on_clicked before it is defined, causing a NameError.
  2. Step 2: Verify other parts

    Button label can be string, axes coordinates are valid, and no slider parameter is needed.
  3. Final Answer:

    The callback function on_press is defined after it is used -> Option B
  4. Quick Check:

    Define callback before registering it [OK]
Quick Trick: Always define callback functions before using them [OK]
Common Mistakes:
  • Defining callback after usage
  • Expecting label to be integer
  • Misunderstanding axes coordinates

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Matplotlib Quizzes