Right now I use a customized FigureCanvasTkAgg and overwrite the draw() and
resize() methods to reset the z-order of the axes before drawing / resizing
the figure and to restore the desired z-order afterwards. This works quite
well but it would be nice to have the picking work like in version <=0.99.3.

----------------------- 
import matplotlib
matplotlib.use('TkAgg')

from numpy import arange, sin, pi, cos
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg,
NavigationToolbar2TkAgg
from matplotlib.figure import Figure
import Tkinter as Tk


root = Tk.Tk()

f = Figure(figsize=(5,4), dpi=100)
ax0 = f.add_subplot(111)
ax1 = ax0.twinx()

t = arange(0.0,3.0,0.01)
s1 = sin(2*pi*t)
s2 = 2*cos(2*pi*t)

ax0.plot(t,s1,color='red', picker=True)
ax1.plot(t,s2,picker=True)
ax0.my_zorder=0
ax1.my_zorder=0.1

class MyFigureCanvasTkAgg(FigureCanvasTkAgg):
    def draw(self):
        ax0 = self.figure.axes[0]
        ax1 = self.figure.axes[1]
        ax0.set_zorder(0)
        ax1.set_zorder(0)
        FigureCanvasTkAgg.draw(self)
        ax0.set_zorder(ax0.my_zorder)
        ax1.set_zorder(ax1.my_zorder)
            
    def resize(self, event):
        ax0 = self.figure.axes[0]
        ax1 = self.figure.axes[1]
        ax0.set_zorder(0)
        ax1.set_zorder(0)
        FigureCanvasTkAgg.resize(self, event)
        ax0.set_zorder(ax0.my_zorder)
        ax1.set_zorder(ax1.my_zorder)


def pick_cb(event):
    if event.artist.get_lw() > 1:
        event.artist.set_lw(1)
    else:
        event.artist.set_lw(3)
    f.canvas.draw()
   

def toggle():
    if ax0.my_zorder == 0:
        ax0.my_zorder=0.1
        ax1.my_zorder=0
    else:
        ax0.my_zorder=0
        ax1.my_zorder=0.1
    ax0.set_zorder(ax0.my_zorder)
    ax1.set_zorder(ax1.my_zorder)


canvas = MyFigureCanvasTkAgg(f, master=root)
canvas.show()
canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)

toolbar = NavigationToolbar2TkAgg(canvas, root)
toolbar.update()
canvas._tkcanvas.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)

canvas.mpl_connect('pick_event', pick_cb)

button = Tk.Button(master=root, text='Toggle', command=toggle)
button.pack(side=Tk.BOTTOM)

Tk.mainloop() 
----------------------- 
-- 
View this message in context: 
http://old.nabble.com/onpick-on-a-2-y-plot-%28-via-twinx%28%29-%29-seems-to-only-allow-picking-of-second-axes%27s-artists-tp25049128p30893570.html
Sent from the matplotlib - users mailing list archive at Nabble.com.


------------------------------------------------------------------------------
The ultimate all-in-one performance toolkit: Intel(R) Parallel Studio XE:
Pinpoint memory and threading errors before they happen.
Find and fix more than 250 security defects in the development cycle.
Locate bottlenecks in serial and parallel code that limit performance.
http://p.sf.net/sfu/intel-dev2devfeb
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to