From: Gökhan S. <gok...@gm...> - 2009-08-04 19:14:27
|
On Tue, Aug 4, 2009 at 11:50 AM, John Hunter <jd...@gm...> wrote: > On Mon, Aug 3, 2009 at 11:38 PM, Gökhan Sever<gok...@gm...> > wrote: > > Hello, > > > > I was wondering if it is possible to hide some data on figures using a > say > > right click option to any of the legend entry and make it temporarily > > hidden/visible to better analyse the rest of the data? > > > > Check this screenshot for example: > > > > http://img25.imageshack.us/img25/9427/datahiding.png > > > > The red data clutters the rest of the figure, and I would like to be able > to > > hide it temporarily so that I can investigate the other two relations > more > > easily. > > > > Any ideas? or alternative solutions? > > It's a nice idea, and should be doable with the pick interface we have > for all mpl artists. Unfortunately, there were a few problems in the > legend implementation which blocked the pick events from hitting the > proxy lines they contained. I just made a few changes to mpl svn HEAD > to support this, and added a new example. > > examples/event_handling/legend_picking.py > > which I'll include below. JJ could you review the changes to legend.py? > > Instructions for checking out svn are at:: > > > http://matplotlib.sourceforge.net/faq/installing_faq.html#install-from-svn > > Here is the example: > > """ > Enable picking on the legend to toggle the legended line on and off > """ > import numpy as np > import matplotlib.pyplot as plt > > t = np.arange(0.0, 0.2, 0.1) > y1 = 2*np.sin(2*np.pi*t) > y2 = 4*np.sin(2*np.pi*2*t) > > fig = plt.figure() > ax = fig.add_subplot(111) > > line1, = ax.plot(t, y1, lw=2, color='red', label='1 hz') > line2, = ax.plot(t, y2, lw=2, color='blue', label='2 hz') > > leg = ax.legend(loc='upper left', fancybox=True, shadow=True) > leg.get_frame().set_alpha(0.4) > > > lines = [line1, line2] > lined = dict() > for legline, realine in zip(leg.get_lines(), lines): > legline.set_picker(5) # 5 pts tolerance > lined[legline] = realine > > def onpick(event): > legline = event.artist > realline = lined[legline] > vis = realline.get_visible() > realline.set_visible(not vis) > fig.canvas.draw() > > fig.canvas.mpl_connect('pick_event', onpick) > > plt.show() > Excellent John. Right what I was seeking for. This will help me a lot to view and analyze my crowded plots. Thanks also Ryan for the additional correction. I have a little question on the code: What is the purpose of using "commas" after line1 and line2 names? I see a little change when I typed them in Ipython, however not exactly sure the real reasoning behind this. In [4]: lines = ax.plot(t, y1, lw=2, color='red', label='1 hz') In [5]: lines Out[5]: [<matplotlib.lines.Line2D object at 0xabce76c>] In [6]: lines, = ax.plot(t, y1, lw=2, color='red', label='1 hz') In [7]: lines Out[7]: <matplotlib.lines.Line2D object at 0xabceaec> Thanks. -- Gökhan |