python - Hiding lines after showing a pyplot figure -
i'm using pyplot display line graph of 30 lines. add way show , hide individual lines on graph. pyplot have menu can edit line properties change color or style, rather clunky when want hide lines isolate 1 you're interested in. ideally, i'd use checkboxes on legend show , hide lines. (similar showing , hiding layers in image editors paint.net) i'm not sure if possible pyplot, open other modules long they're easy distribute.
if you'd like, can hook callback legend show/hide lines when they're clicked. there's simple example here: http://matplotlib.org/examples/event_handling/legend_picking.html
here's "fancier" example should work without needing manually specify relationship of lines , legend markers (also has few more features).
import numpy np import matplotlib.pyplot plt def main(): x = np.arange(10) fig, ax = plt.subplots() in range(1, 31): ax.plot(x, * x, label=r'$y={}x$'.format(i)) ax.legend(loc='upper left', bbox_to_anchor=(1.05, 1), ncol=2, borderaxespad=0) fig.subplots_adjust(right=0.55) fig.suptitle('right-click hide all\nmiddle-click show all', va='top', size='large') interactive_legend().show() def interactive_legend(ax=none): if ax none: ax = plt.gca() if ax.legend_ none: ax.legend() return interactivelegend(ax.legend_) class interactivelegend(object): def __init__(self, legend): self.legend = legend self.fig = legend.axes.figure self.lookup_artist, self.lookup_handle = self._build_lookups(legend) self._setup_connections() self.update() def _setup_connections(self): artist in self.legend.texts + self.legend.legendhandles: artist.set_picker(10) # 10 points tolerance self.fig.canvas.mpl_connect('pick_event', self.on_pick) self.fig.canvas.mpl_connect('button_press_event', self.on_click) def _build_lookups(self, legend): labels = [t.get_text() t in legend.texts] handles = legend.legendhandles label2handle = dict(zip(labels, handles)) handle2text = dict(zip(handles, legend.texts)) lookup_artist = {} lookup_handle = {} artist in legend.axes.get_children(): if artist.get_label() in labels: handle = label2handle[artist.get_label()] lookup_handle[artist] = handle lookup_artist[handle] = artist lookup_artist[handle2text[handle]] = artist lookup_handle.update(zip(handles, handles)) lookup_handle.update(zip(legend.texts, handles)) return lookup_artist, lookup_handle def on_pick(self, event): handle = event.artist if handle in self.lookup_artist: artist = self.lookup_artist[handle] artist.set_visible(not artist.get_visible()) self.update() def on_click(self, event): if event.button == 3: visible = false elif event.button == 2: visible = true else: return artist in self.lookup_artist.values(): artist.set_visible(visible) self.update() def update(self): artist in self.lookup_artist.values(): handle = self.lookup_handle[artist] if artist.get_visible(): handle.set_visible(true) else: handle.set_visible(false) self.fig.canvas.draw() def show(self): plt.show() if __name__ == '__main__': main()
this allows click on legend items toggle corresponding artists on/off. example, can go this:
to this:
Comments
Post a Comment