Last active
October 17, 2019 11:10
-
-
Save tobbe11/16d4d1e374812f3090581b27f19b5424 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/env python | |
| import numpy as np | |
| import matplotlib | |
| import matplotlib.pyplot as plt | |
| from matplotlib.widgets import LassoSelector, RectangleSelector, PolygonSelector | |
| matplotlib.use("Qt5Agg") # This program works with Qt only | |
| fig, ax1 = plt.subplots() | |
| t = np.linspace(0, 10, 200) | |
| lines = [] | |
| for freq in np.linspace(1, 10, 30): | |
| lines.extend(plt.plot(t, np.sin(2*np.pi*freq*t), label="f={}".format(freq))) | |
| ### control panel ### | |
| from PyQt5 import QtCore, QtWidgets | |
| from PyQt5.QtCore import Qt | |
| root = fig.canvas.manager.window | |
| panel = QtWidgets.QWidget() | |
| hbox = QtWidgets.QHBoxLayout(panel) | |
| list_box = QtWidgets.QListWidget(parent=panel) | |
| list_box.setSelectionMode(QtWidgets.QListWidget.MultiSelection) | |
| list_box.setFlow(QtWidgets.QListView.LeftToRight) | |
| list_box.setWrapping(True) | |
| list_box.setGridSize(QtCore.QSize(150, 20)) | |
| for line in lines: | |
| list_box.addItem(line.get_label()) | |
| button_update = QtWidgets.QPushButton("update", panel) | |
| def on_update(event): | |
| selected_labels = set([item.text() for item in list_box.selectedItems()]) | |
| for line in lines: | |
| line.set_visible(line.get_label() in selected_labels) | |
| fig.canvas.draw() | |
| def onLassoSelect(selected_path): | |
| print(len(selected_path), selected_path) | |
| poly = PolygonSelector(ax=ax1, onselect=onLassoSelect) | |
| button_update.clicked.connect(on_update) | |
| hbox.addWidget(list_box) | |
| hbox.addWidget(button_update) | |
| panel.setLayout(hbox) | |
| dock = QtWidgets.QDockWidget("control", root) | |
| root.addDockWidget(Qt.BottomDockWidgetArea, dock) | |
| dock.setWidget(panel) | |
| ###################### | |
| fig.show() | |
| plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment