<jupyter_start><jupyter_text>See [Rectangle Selector](https://matplotlib.org/stable/gallery/widgets/rectangle_selector.html).<jupyter_code>%matplotlib widget
import matplotlib<jupyter_output><empty_output><jupyter_text>Rectangle SelectorDo a mouseclick somewhere, move the mouse to some destination, releasethe button.  This class gives click- and release-events and also drawsa line or a box from the click-point to the actual mouseposition(within the same axes) until the button is released.  Within themethod ``self.ignore()`` it is checked whether the button from eventpressand eventrelease are the same.<jupyter_code>from matplotlib.widgets import RectangleSelector
import numpy as np
import matplotlib.pyplot as plt


def line_select_callback(eclick, erelease):
    """
    Callback for line selection.

    *eclick* and *erelease* are the press and release events.
    """
    x1, y1 = eclick.xdata, eclick.ydata
    x2, y2 = erelease.xdata, erelease.ydata
    print(f"({x1:3.2f}, {y1:3.2f}) --> ({x2:3.2f}, {y2:3.2f})")
    print(f" The buttons you used were: {eclick.button} {erelease.button}")


def toggle_selector(event):
    print(' Key pressed.')
    if event.key == 't':
        if toggle_selector.RS.active:
            print(' RectangleSelector deactivated.')
            toggle_selector.RS.set_active(False)
        else:
            print(' RectangleSelector activated.')
            toggle_selector.RS.set_active(True)


fig, ax = plt.subplots()
N = 100000  # If N is large one can see improvement by using blitting.
x = np.linspace(0, 10, N)

ax.plot(x, np.sin(2*np.pi*x))  # plot something
ax.set_title(
    "Click and drag to draw a rectangle.\n"
    "Press 't' to toggle the selector on and off.")

# drawtype is 'box' or 'line' or 'none'
toggle_selector.RS = RectangleSelector(ax, line_select_callback,
                                       drawtype='box', useblit=True,
                                       button=[1, 3],  # disable middle button
                                       minspanx=5, minspany=5,
                                       spancoords='pixels',
                                       interactive=True)
fig.canvas.mpl_connect('key_press_event', toggle_selector)
# plt.show()
plt.isinteractive()<jupyter_output><empty_output><jupyter_text>Print toolbar items<jupyter_code>from matplotlib.widgets import RectangleSelector
import numpy as np
import matplotlib.pyplot as plt


def line_select_callback(eclick, erelease):
    """
    Callback for line selection.

    *eclick* and *erelease* are the press and release events.
    """
    x1, y1 = eclick.xdata, eclick.ydata
    x2, y2 = erelease.xdata, erelease.ydata
    print(f"({x1:3.2f}, {y1:3.2f}) --> ({x2:3.2f}, {y2:3.2f})")
    print(f" The buttons you used were: {eclick.button} {erelease.button}")


def toggle_selector(event):
    print(' Key pressed.')
    if event.key == 't':
        if toggle_selector.RS.active:
            print(' RectangleSelector deactivated.')
            toggle_selector.RS.set_active(False)
        else:
            print(' RectangleSelector activated.')
            toggle_selector.RS.set_active(True)


fig, ax = plt.subplots()
N = 100000  # If N is large one can see improvement by using blitting.
x = np.linspace(0, 10, N)

ax.plot(x, np.sin(2*np.pi*x))  # plot something
# ax.set_title(
#     "Click and drag to draw a rectangle.\n"
#     "Press 't' to toggle the selector on and off.")

# drawtype is 'box' or 'line' or 'none'
# toggle_selector.RS = RectangleSelector(ax, line_select_callback,
#                                        drawtype='box', useblit=True,
#                                        button=[1, 3],  # disable middle button
#                                        minspanx=5, minspany=5,
#                                        spancoords='pixels',
#                                        interactive=True)
# fig.canvas.mpl_connect('key_press_event', toggle_selector)
# plt.show()

toolbar = fig.canvas.manager.toolbar
tb=toolbar.toolitems
print(tb)<jupyter_output><empty_output><jupyter_text>See Donald's answer at [How to modify the navigation toolbar easily in a matplotlib figure window?](https://stackoverflow.com/questions/12695678/how-to-modify-the-navigation-toolbar-easily-in-a-matplotlib-figure-window) for last 3 lines above.  Try to pop zoom tool (failed)<jupyter_code>def line_select_callback(eclick, erelease):
    """
    Callback for line selection.

    *eclick* and *erelease* are the press and release events.
    """
    x1, y1 = eclick.xdata, eclick.ydata
    x2, y2 = erelease.xdata, erelease.ydata
    print(f"({x1:3.2f}, {y1:3.2f}) --> ({x2:3.2f}, {y2:3.2f})")
    print(f" The buttons you used were: {eclick.button} {erelease.button}")


def toggle_selector(event):
    print(' Key pressed.')
    if event.key == 't':
        if toggle_selector.RS.active:
            print(' RectangleSelector deactivated.')
            toggle_selector.RS.set_active(False)
        else:
            print(' RectangleSelector activated.')
            toggle_selector.RS.set_active(True)


fig, ax = plt.subplots()

toolbar = fig.canvas.manager.toolbar
tb=toolbar.toolitems
print(tb)
tb.pop(4)
# matplotlib.backend_tools.add_tools_to_manager(fig.canvas.manager, tools={'grid': matplotlib.backend_tools.ToolGrid})
print(tb)

N = 100000  # If N is large one can see improvement by using blitting.
x = np.linspace(0, 10, N)

ax.plot(x, np.sin(2*np.pi*x))  # plot something


fig.canvas.manager.toolbar.zoom
fig.canvas.manager.toolbar.press_zoom<jupyter_output><empty_output><jupyter_text>Test deleting/popping item from list<jupyter_code>temp = ['a', 'b', 'c', 'd', 'e', 'f']
print(temp)
del temp[4]
print(temp)
temp = ['a', 'b', 'c', 'd', 'e', 'f']
print(temp)
temp.pop(4)
print(temp)<jupyter_output>['a', 'b', 'c', 'd', 'e', 'f']
['a', 'b', 'c', 'd', 'f']<jupyter_text>See [Backend Tools API](https://matplotlib.org/stable/api/backend_tools_api.html)  Other method to delete toolSee Aking's answer at [How to remove toolbar buttons from matplotlib](https://stackoverflow.com/questions/55779944/how-to-remove-toolbar-buttons-from-matplotlib).<jupyter_code>def line_select_callback(eclick, erelease):
    """
    Callback for line selection.

    *eclick* and *erelease* are the press and release events.
    """
    x1, y1 = eclick.xdata, eclick.ydata
    x2, y2 = erelease.xdata, erelease.ydata
    print(f"({x1:3.2f}, {y1:3.2f}) --> ({x2:3.2f}, {y2:3.2f})")
    print(f" The buttons you used were: {eclick.button} {erelease.button}")


def toggle_selector(event):
    print(' Key pressed.')
    if event.key == 't':
        if toggle_selector.RS.active:
            print(' RectangleSelector deactivated.')
            toggle_selector.RS.set_active(False)
        else:
            print(' RectangleSelector activated.')
            toggle_selector.RS.set_active(True)


fig, ax = plt.subplots()

toolbar = plt.get_current_fig_manager().toolbar
tb=toolbar.toolitems
print(tb)
unwanted_buttons = ['Zoom','Save']
for x in toolbar.actions():
    if x.text() in unwanted_buttons:
        toolbar.removeAction(x)
print(tb)

N = 100000  # If N is large one can see improvement by using blitting.
x = np.linspace(0, 10, N)

ax.plot(x, np.sin(2*np.pi*x))  # plot something<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12941823683124742, 'fraction_numerical': 0.018154311649016642, 'mean_word_length': 3.106154714850367, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 28, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '246042', 'license': 'MIT', 'n_tokens_mistral': 2469, 'n_tokens_neox': 2387, 'n_words': 591, 'path': 'python/matplotlib/toolbar_and_rectangle_selector.ipynb', 'repo_name': 'gregnordin/python-notes_to_self', 'size': '7271'}