Reputation: 834
I want to enable picker event (clicking on a dot and its coordinates are printed), but on a plot with a secondary y-axis.
For example, this is adopted from the twinx example. The picker event is somehow only enabled on the second axis (sine line):
import numpy as np
import matplotlib.pyplot as plt
fig, ax1 = plt.subplots()
t = np.arange(0.01, 10.0, 0.01)
s1 = np.exp(t)
ax1.plot(t, s1, 'b.',picker=5)
ax1.set_xlabel('time (s)')
# Make the y-axis label, ticks and tick labels match the line color.
ax1.set_ylabel('exp', color='b')
ax1.tick_params('y', colors='b')
ax2 = ax1.twinx()
s2 = np.sin(2 * np.pi * t)
ax2.plot(t, s2, 'r.',picker=5)
ax2.set_ylabel('sin', color='r')
ax2.tick_params('y', colors='r')
def onpick1(event):
thisline = event.artist
xdata = thisline.get_xdata()
ydata = thisline.get_ydata()
ind = event.ind
print('Point: ', zip(np.take(xdata, ind), np.take(ydata, ind)))
fig.canvas.mpl_connect('pick_event', onpick1)
fig.tight_layout()
plt.show()
Upvotes: 2
Views: 1016
Reputation: 33
you just need to change the zorder:
ax.set_zorder(axTwin.get_zorder() + 1)
Upvotes: 2
Reputation: 13031
This is the standard behaviour of twinx
. From the documentation:
For those who are 'picking' artists while using twinx, pick events are only called for the artists in the top-most axes.
Upvotes: 2