How to generate multiple plots by clicking a single plot for more information using clickable python events

Viewed 1025

I am in the process of developing an application which can generate a 2nd plot by clicking data point in the 1st plot. I am using events to accomplish this.

Question:

  1. How to generate a 3rd plot by clicking a 2nd plot data point? Is it possible to accomplish to this?
  2. How to generate a simpler 3 layer synthetic data?

Code I have so far:

"""
compute the mean and stddev of 100 data sets and plot mean vs stddev.
When you click on one of the mu, sigma points, plot the raw data from
the dataset that generated the mean and stddev
"""
import numpy as np
import matplotlib.pyplot as plt

X = np.random.rand(100, 1000)
xs = np.mean(X, axis=1)
ys = np.std(X, axis=1)

fig = plt.figure()
ax = fig.add_subplot(211)
bx = fig.add_subplot(212)

# ax.set_title('click on point to plot time series')
# bx.set_title('click on point to plot time series')


line, = ax.plot(xs, ys, 'bs', picker=5)  # 5 points tolerance
line1, = bx.plot(xs, ys, 'bo', picker=5)  # 5 points tolerance

def onpick(event):

    if event.artist!=line: return True

    N = len(event.ind)
    if not N: return True


    figi = plt.figure()
    for subplotnum, dataind in enumerate(event.ind):

        ax = figi.add_subplot(N,1,subplotnum+1)
        ax.plot(X[dataind])
        ax.text(0.05, 0.9, 'mu=%1.3f\nsigma=%1.3f'%(xs[dataind], ys[dataind]),
                transform=ax.transAxes, va='top')
        ax.set_ylim(-0.5, 1.5)

        bx = figi.add_subplot(N,1,subplotnum+1)
        bx.plot(X[dataind])
        bx.text(0.05, 0.9, 'mu=%1.3f\nsigma=%1.3f'%(xs[dataind], ys[dataind]),
                transform=ax.transAxes, va='top')
        bx.set_ylim(-0.5, 1.5)

    figi.show()
    return True

fig.canvas.mpl_connect('pick_event', onpick)

plt.show()

1st plot

By clicking data points on the first plot, 2nd plot will be generated.

2nd plot

Now expectations are to generate a 3rd plot clicking the 2nd plot data point.

I have researched on this, but I am not successful :-( :-(

If my approach is wrong, please do suggest the any other alternative.

1 Answers
Related