0
0
Matplotlibdata~10 mins

Cursor and event handling in Matplotlib - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the module needed for plotting.

Matplotlib
import [1] as plt
Drag options to blanks, or click blank then click option'
Amatplotlib.pyplot
Bnumpy
Cseaborn
Dpandas
Attempts:
3 left
💡 Hint
Common Mistakes
Importing numpy or pandas instead of matplotlib.pyplot.
Using seaborn which is a different plotting library.
2fill in blank
medium

Complete the code to create a figure and axis for plotting.

Matplotlib
fig, ax = plt.[1]()
Drag options to blanks, or click blank then click option'
Aplot
Bsubplots
Cfigure
Dshow
Attempts:
3 left
💡 Hint
Common Mistakes
Using plt.figure() which returns only the figure.
Using plt.plot() which creates a plot but not figure and axes separately.
3fill in blank
hard

Fix the error in the event connection code to correctly connect the cursor move event.

Matplotlib
cid = fig.canvas.[1]('motion_notify_event', on_move)
Drag options to blanks, or click blank then click option'
Aadd_event
Bconnect_event
Cconnect
Dbind
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect method names like connect_event or add_event.
Trying to use bind which is not a matplotlib method.
4fill in blank
hard

Fill both blanks to create a cursor object and enable it on the axis.

Matplotlib
cursor = Cursor([1], useblit=True, color='red', linewidth=1)
cursor.[2]()
Drag options to blanks, or click blank then click option'
Aax
Bfig
Cdraw
Denable
Attempts:
3 left
💡 Hint
Common Mistakes
Passing figure instead of axis to Cursor.
Using a non-existent method like enable() instead of draw().
5fill in blank
hard

Fill all three blanks to create a function that prints cursor coordinates on mouse move.

Matplotlib
def on_move(event):
    if event.inaxes == [1]:
        x, y = event.[2], event.[3]
        print(f"Cursor at: ({x:.2f}, {y:.2f})")
Drag options to blanks, or click blank then click option'
Aax
Bxdata
Cydata
Dfig
Attempts:
3 left
💡 Hint
Common Mistakes
Checking event.inaxes against figure instead of axis.
Using event.x and event.y which are pixel positions, not data coordinates.