Home > Net >  How to get ax plot id for matplotlib RectangleSelector callback?
How to get ax plot id for matplotlib RectangleSelector callback?

Time:12-02

I have multiple ax plot with RectangleSelector and its callback as below.

from matplotlib.widgets import RectangleSelector
import numpy as np
import matplotlib.pyplot as plt

def select_callback(eclick, erelease):
    x1, y1 = eclick.xdata, eclick.ydata
    x2, y2 = erelease.xdata, erelease.ydata

fig = plt.figure(constrained_layout=True)
axs = fig.subplots(4)

x = np.linspace(0, 10, 1000)

selectors = []
for idx,ax in enumerate(axs):
    ax.plot(x, np.sin(2*np.pi*x))  # plot something
    selectors.append(RectangleSelector(
        ax, select_callback,
        useblit=True,
        button=[1, 3],  # disable middle button
        minspanx=5, minspany=5,
        spancoords='pixels',
        interactive=True))

plt.show() 

I can draw and get callback perfectly but I would like to capture which ax is working on, for example its id. Any advice or guidance on this would be greatly appreciated, Thanks.

CodePudding user response:

You can use a enter image description here

  • Related