I am trying to get all values of the object pg.InfiniteLine()
using getXPos()
method.
I tried the following (this code snippet is set inside of class MainWindow(QMainWindow)
):
inf = pg.InfiniteLine(movable=True, angle=90, label='x={value:.3f}:{value:.3f}',
labelOpts={'position':0.1, 'color': (200,200,100), 'fill': (200,200,200,50), 'movable': True})
inf.setPos([0,0])
self.graphWidget.addItem(inf)
Note: value
in label='{value:.3f}:{value:.3f}'
is after converting a list of strings t_string_list[:5] = ['0:0', '0:1', '0:2', '0:3', '0:4']
(of format H:M) to minutes stored in a variable t_time_value
.
What I am trying to do is that label
be set back to x=H:M
displayed in the graph. To do that, I am trying to get all values of getXPos()
for me to use Converting a float to hh:mm format.
I tried to do print(inf.getXPos())
but prints only 0
. I also tried print(inf.getXPos(t_time_value))
I got TypeError: getXPos() takes 1 positional argument but 2 were given
. I am not sure how to label the InfiniteLine
with x=H:M
where H:M should be values using label='{value:.3f}:{value:.3f}'.format(*divmod(pos / 60, 1))
argument of inf
where I am facing difficulties to define pos
.
This post is continuous to How to plot hour:min time in ISO 8601 time format?
Note: label='{value:.3f}:{value:.3f}'
already gives correct positions whenever I move the line, but I couldn't do like this label='{value:.3f}:{value:.3f}'.format(value/60, 1)
because value
isn't defined.
CodePudding user response:
You cannot use evaluation in a basic format string, nor try to "get all values", since graphs use real numbers: you can have infinite points between 0 and 1.
The possible solution is to add the InfLineLabel
manually, using a subclass that overrides the default behavior of valueChanged
:
class InfTimeLabel(pg.InfLineLabel):
def valueChanged(self):
if not self.isVisible():
return
value = self.line.value()
h, m = divmod(value, 60)
text = '{}:{:06.3f}'.format(int(h), m)
self.setText(text)
self.updatePosition()
Then add it like this:
infLine = pg.InfiniteLine(movable=True, angle=90)
opts = {
'position': 0.1,
'color': (200, 200, 100),
'fill': (200, 200, 200, 50),
'movable': True
}
infLine.label = InfTimeLabel(infLine, **opts)
self.graphWidget.addItem(infLine)