Ignore matplotlib cursor widget when choosing a toolbar widget?

I use the cursor widget in the Matplotlib interactive graph as follows:

cursor = Cursor(ax1, useblit=True, color='red', linewidth=1)
cid = fig.canvas.mpl_connect('button_press_event', on_click)

It works well. The function on_clicktakes x, y click locations and makes some additional graphs. The main things.

When I activate the zoom tool, I also fix the click. Do I need to associate the activation and deactivation of the key stroke with widgets a la the RectangleSelector or is there a method that knows the state of the toolbar elements?

An example of turning on / off the selector from the RectangleSelector example:

def toggle_selector(event):
    if event.key in ['Q','q'] and toggle_selector.RS.active:
        toggle_selector.RS.set_active(False)
    if event.key in ['A', 'a'] and not toggle_selector.RS.active:
        toggle_selector.RS.set_active(True)
+4
source share
2 answers

,

fig.canvas.manager.toolbar._active is None

True, ( ).

, , . -, _* .

+8

toolmanager , .

import matplotlib.pyplot as plt
plt.rcParams['toolbar'] = 'toolmanager'

fig, ax = plt.subplots()

def on_click(evt):
    state = fig.canvas.manager.toolbar.toolmanager.active_toggle["default"]
    if state is None:
        print("no tool selected")
    else:
        print(f"{state} selected")

cid = fig.canvas.mpl_connect('button_press_event', on_click)

plt.show()
0

All Articles