pythonevent-handlingmatplotlibcolor-mapping

Event Connections and subplots in matplotlib


I have what seems to be a simple task yet I am not sure how and where to start. What I currently have is a series of subplots displayed on one figure. Now I want to add/connect an event handler on each subplot, such that when the user clicks on one of the subplots the plot that was selected would be opened in a separate figure/window.
I want to know if this is possible and if someone could formulate a small simple code to illustrate how this can be done. I should also mention that the only type of plot that I am using and interested in are colormaps (using imshow()).


Solution

  • You should read this tutorial.

    Basically you need to define function which takes one arguement event and then attach it to your figure's canvas:

    def open_new_figure(event):
        if event.inaxes is not None:
            ax = event.inaxes
            # you now have the axes object for that the user clicked on
            # you can use ax.children() to figure out which img artist is in this
            # axes and extract the data from it
    
    cid = fig.canvas.mpl_connect('button_press_event', open_new_figure)