CoolFace
Apppublic

Aluode/PerceptionLabPortable

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
text.py54 linesDownload Raw Back to examples
1"""2This example shows how to insert text into a scene using TextItem. This class 3is for displaying text that is anchored to a particular location in the data4coordinate system, but which is always displayed unscaled. 5 6For text that scales with the data, use QTextItem. 7For text that can be placed in a layout, use LabelItem.8"""9 10import numpy as np11 12import pyqtgraph as pg13from pyqtgraph.Qt import QtCore14 15x = np.linspace(-20, 20, 1000)16y = np.sin(x) / x17plot = pg.plot()   ## create an empty plot widget18plot.setYRange(-1, 2)19plot.setWindowTitle('pyqtgraph example: text')20curve = plot.plot(x,y)  ## add a single curve21 22## Create text object, use HTML tags to specify color/size23text = pg.TextItem(html='<div style="text-align: center"><span style="color: #FFF;">This is the</span><br><span style="color: #FF0; font-size: 16pt;">PEAK</span></div>', anchor=(-0.3,0.5), angle=45, border='w', fill=(0, 0, 255, 100))24plot.addItem(text)25text.setPos(0, y.max())26 27## Draw an arrowhead next to the text box28arrow = pg.ArrowItem(pos=(0, y.max()), angle=-45)29plot.addItem(arrow)30 31 32## Set up an animated arrow and text that track the curve33curvePoint = pg.CurvePoint(curve)34plot.addItem(curvePoint)35text2 = pg.TextItem("test", anchor=(0.5, -1.0))36text2.setParentItem(curvePoint)37arrow2 = pg.ArrowItem(angle=90)38arrow2.setParentItem(curvePoint)39 40## update position every 10ms41index = 042def update():43    global curvePoint, index44    index = (index + 1) % len(x)45    curvePoint.setPos(float(index)/(len(x)-1))46    text2.setText('[%0.1f, %0.1f]' % (x[index], y[index]))47    48timer = QtCore.QTimer()49timer.timeout.connect(update)50timer.start(10)51 52if __name__ == '__main__':53    pg.exec()54 
Aluode/PerceptionLabPortable · CoolFace