CoolFace
Apppublic

Aluode/PerceptionLabPortable

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
Legend.py78 linesDownload Raw Back to examples
1"""2Demonstrates basic use of LegendItem3"""4 5from pyqtgraph.Qt.QtWidgets import QInputDialog6 7import numpy as np8 9import pyqtgraph as pg10 11win = pg.plot()12win.setWindowTitle('pyqtgraph example: BarGraphItem')13 14# # option1: only for .plot(), following c1,c2 for example-----------------------15# win.addLegend(frame=False, colCount=2)16 17# bar graph18x = np.arange(10)19y = np.sin(x+2) * 320bg1 = pg.BarGraphItem(x=x, height=y, width=0.3, brush='b', pen='w', name='bar')21win.addItem(bg1)22 23# curve24c1 = win.plot([np.random.randint(0,8) for i in range(10)], pen='r', symbol='t', symbolPen='r', symbolBrush='g', name='curve1')25c2 = win.plot([2,1,4,3,1,3,2,4,3,2], pen='g', fillLevel=0, fillBrush=(255,255,255,30), name='curve2')26 27# scatter plot28s1 = pg.ScatterPlotItem(size=10, pen=pg.mkPen(None), brush=pg.mkBrush(255, 255, 255, 120), name='scatter')29spots = [{'pos': [i, np.random.randint(-3, 3)], 'data': 1} for i in range(10)]30s1.addPoints(spots)31win.addItem(s1)32 33# # option2: generic method------------------------------------------------34legend = pg.LegendItem((80,60), offset=(70,20))35legend.setParentItem(win.graphicsItem())36legend.addItem(bg1, 'bar')37legend.addItem(c1, 'curve1')38legend.addItem(c2, 'curve2')39legend.addItem(s1, 'scatter')40 41 42def legendDoubleClicked(legend, event):43    # update legend font size and redraw44    current_font_size = int(legend.labelTextSize().replace('pt', ''))45 46    dialog = QInputDialog()47    dialog.setWindowTitle("Input Dialog")48    dialog.setInputMode(QInputDialog.InputMode.IntInput)49    dialog.setLabelText('Enter Label Font Size:')50    dialog.setIntValue(current_font_size)51 52    if dialog.exec() == QInputDialog.DialogCode.Accepted:53        font_size = dialog.intValue()54        legend.setLabelTextSize('%dpt' % font_size)55        legend_items = legend.items.copy()56        legend.clear()57        # re-add items to update labels and redraw58        for sample, label in legend_items:59            legend.addItem(sample.item, label.text)60 61    event.accept()62 63 64def legendSampleClicked(plot_data_item):65    # indicate plot data item visibility66    if plot_data_item.isVisible():67        print('"%s" is visible' % plot_data_item.name())68    else:69        print('"%s" is not visible' % plot_data_item.name())70 71 72legend.sigDoubleClicked.connect(legendDoubleClicked)73legend.sigSampleClicked.connect(legendSampleClicked)74 75 76if __name__ == '__main__':77    pg.exec()78 
Aluode/PerceptionLabPortable · CoolFace