2012-10-19 03:18:20 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2013-02-25 04:09:03 +00:00
|
|
|
"""
|
|
|
|
Demonstrates basic use of LegendItem
|
|
|
|
|
|
|
|
"""
|
2012-10-19 03:18:20 +00:00
|
|
|
import initExample ## Add path to library (just for examples; you do not need this)
|
|
|
|
|
|
|
|
import pyqtgraph as pg
|
|
|
|
from pyqtgraph.Qt import QtCore, QtGui
|
2018-12-21 06:47:23 +00:00
|
|
|
import numpy as np
|
2012-10-19 03:18:20 +00:00
|
|
|
|
2018-12-21 06:47:23 +00:00
|
|
|
win = pg.plot()
|
|
|
|
win.setWindowTitle('pyqtgraph example: BarGraphItem')
|
2012-10-19 03:18:20 +00:00
|
|
|
|
2018-12-21 06:47:23 +00:00
|
|
|
# # option1: only for .plot(), following c1,c2 for example-----------------------
|
2021-02-06 07:00:34 +00:00
|
|
|
# win.addLegend(frame=False, colCount=2)
|
2018-12-21 06:47:23 +00:00
|
|
|
|
|
|
|
# bar graph
|
|
|
|
x = np.arange(10)
|
|
|
|
y = np.sin(x+2) * 3
|
|
|
|
bg1 = pg.BarGraphItem(x=x, height=y, width=0.3, brush='b', pen='w', name='bar')
|
|
|
|
win.addItem(bg1)
|
|
|
|
|
|
|
|
# curve
|
|
|
|
c1 = win.plot([np.random.randint(0,8) for i in range(10)], pen='r', symbol='t', symbolPen='r', symbolBrush='g', name='curve1')
|
|
|
|
c2 = win.plot([2,1,4,3,1,3,2,4,3,2], pen='g', fillLevel=0, fillBrush=(255,255,255,30), name='curve2')
|
|
|
|
|
|
|
|
# scatter plot
|
|
|
|
s1 = pg.ScatterPlotItem(size=10, pen=pg.mkPen(None), brush=pg.mkBrush(255, 255, 255, 120), name='scatter')
|
|
|
|
spots = [{'pos': [i, np.random.randint(-3, 3)], 'data': 1} for i in range(10)]
|
|
|
|
s1.addPoints(spots)
|
|
|
|
win.addItem(s1)
|
|
|
|
|
|
|
|
# # option2: generic method------------------------------------------------
|
|
|
|
legend = pg.LegendItem((80,60), offset=(70,20))
|
|
|
|
legend.setParentItem(win.graphicsItem())
|
|
|
|
legend.addItem(bg1, 'bar')
|
|
|
|
legend.addItem(c1, 'curve1')
|
|
|
|
legend.addItem(c2, 'curve2')
|
|
|
|
legend.addItem(s1, 'scatter')
|
2012-10-19 03:18:20 +00:00
|
|
|
|
2012-12-05 05:25:45 +00:00
|
|
|
if __name__ == '__main__':
|
2021-03-22 18:17:12 +00:00
|
|
|
pg.mkQApp().exec_()
|