1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
import sys
import numpy as np
from PyQt4.QtGui import QWidget, QVBoxLayout, QApplication
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
import matplotlib.pyplot as plt
def interactive_legend(ax=None):
if ax is None:
ax = plt.gca()
if ax.legend_ is None:
ax.legend()
return InteractiveLegend(ax.legend_)
class InteractiveLegend(object):
@classmethod
def __init__(self, legend):
# super(InteractiveLegend, self).__init__()
# InteractiveLegend.__init__()
self.legend = legend
self.fig = legend.axes.figure
self.lookup_artist, self.lookup_handle = InteractiveLegend._build_lookups(legend)
self._setup_connections()
self.update()
@classmethod
def _setup_connections(self):
for 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)
@staticmethod
def _build_lookups(legend):
labels = [t.get_text() for t in legend.texts]
handles = legend.legendHandles
label2handle = dict(zip(labels, handles))
handle2text = dict(zip(handles, legend.texts))
lookup_artist = {}
lookup_handle = {}
for 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
@classmethod
def on_pick(self, event):
print('on_pick')
handle = event.artist
if handle in self.lookup_artist:
artist = self.lookup_artist[handle]
artist.set_visible(not artist.get_visible())
self.update()
@classmethod
def on_click(self, event):
print('on_click')
if event.button == 3:
visible = False
elif event.button == 2:
visible = True
else:
return
for artist in self.lookup_artist.values():
artist.set_visible(visible)
self.update()
@classmethod
def update(self):
for 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()
class Fenetre(QWidget):
def __init__(self):
QWidget.__init__(self)
self.layout = QVBoxLayout()
self.fig = plt.figure()
self.axes = self.fig.add_subplot(111)
self.x = np.linspace(-np.pi, np.pi, 30)
self.y = np.cos(self.x)
self.line, = self.axes.plot(self.x, self.y, label='aaa')
self.canvas = FigureCanvas(self.fig)
self.layout.addWidget(self.canvas)
interactive_legend()
self.setLayout(self.layout)
self.show()
app = QApplication.instance()
if not app:
app = QApplication(sys.argv)
fen = Fenetre()
app.exec_() |
Partager