Hi,
I think what you are asking is you'd like the label text to always remain
inside the region, rather than being at a fixed point. So use the SetPos
method (it's inherited from QGraphicsItem) to move it on update.
Also, here's an example with couple of other ideas: why not update the
label whenever the region is changed?
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
import numpy as np
import pyqtgraph as pg
from pyqtgraph.graphicsItems.InfiniteLine import InfLineLabel
app = QtWidgets.QApplication(sys.argv)
my_data = np.random.random(10)
win = pg.GraphicsWindow()
pl = win.addPlot(row=1, col=0)
pl.plot(my_data)
ti = pg.TextItem('', anchor=(0,1.1), color=(146, 146, 146), fill='w')
pl.addItem(ti)
lr = pg.LinearRegionItem(orientation=pg.LinearRegionItem.Horizontal)
pl.addItem(lr)
# Here's another idea:
# Add the value label functionality to the InfiniteLine components of the
LinearRegionItem
lr.lines[0].label = InfLineLabel(lr.lines[0], text="{value:0.3f}")
lr.lines[1].label = InfLineLabel(lr.lines[1], text="{value:0.3f}")
def update_regiontext():
lo,hi = lr.getRegion()
ti.setText("{:0.3f} {:0.3f}".format(lo, hi))
ti.setPos(0, lo)
update_regiontext()
# If you really want to only update on click then uncomment code below
# def on_click(ev):
# if (ev.button() == QtCore.Qt.LeftButton):
# update_regiontext()
# ev.accept()
# pl.vb.mouseClickEvent = on_click
# Otherwise, this will update whenever the region is changed
lr.sigRegionChanged.connect(update_regiontext)
win.show()
sys.exit(app.exec_())
Patrick
On Thursday, 17 January 2019 06:26:27 UTC+10:30, Greydon Gilmore wrote:
>
> Hello,
>
> I am using pg.LinearRegionItem to select an area on the plot.
> Once I have selected the area I have set up a mouse event, that when left
> button is clicked the region borders will be displayed using pg.TextItem.
>
> However, I want to be able to replace the pg.TextItem is the
> LinearRegionItem is changed and a new mouse click is detected
>
> win = pg.GraphicsWindow()
> pl = win.addPlot(row=1, col=0)
> pl.plot(my_data)
> lr = pg.LinearRegionItem(orientation=pg.LinearRegionItem.Horizontal)
> pl.addItem(lr)
>
> def on_click(ev):
> if (ev.button() == QtCore.Qt.LeftButton):
> lo,hi = lr.getRegion()
> print (lo,hi)
> ti = pg.TextItem('', anchor=(0,1.1), color=(146, 146, 146), fill='w')
> pl.addItem(ti)
> ti.setText(str(round(lo,3))+ ' ' + str(round(hi,3)))
> ev.accept()
>
> pl.vb.mouseClickEvent = on_click
>
> Any help would be appreciated!
>
> Greydon
>
--
You received this message because you are subscribed to the Google Groups
"pyqtgraph" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/pyqtgraph/22e9e3f7-ded6-4373-a99d-44ec92783a69%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.