InfLineLabels

275 views
Skip to first unread message

truef...@gmail.com

unread,
Apr 11, 2019, 10:45:12 AM4/11/19
to pyqtgraph
I am annotating key events in my plots by overlaying them with static vertical lines with labels. A mouse hover event expands the label to provide more data. Both labels (default and expanded) contain multiple lines.
This is done with the following approach:

class VerticalLineAnnotation(pyqtgraph.InfiniteLine):
...
def hoverEvent(self, ev):
    if ev.isExit():
        self._expand_label(False)
    else:
        self._expand_label(True)
    super().hoverEvent(ev)
...
def _expand_label(self, expand):
    if self._expanded_label == expand:
        return
    self._expanded_label = expand
    if expand:
        self.label.setFormat(self._hover_label)
    else:
        self.label.setFormat(self._default_label)
    self.update()


However, I currently must also adjust the position of the label since its height changes:

def _expand_label(self, expand):
    if self._expanded_label == expand:
        return
    self._expanded_label = expand
    if expand:
        self._label_pos = self.label.orthoPos
        self.label.setFormat(self._hover_label)
        self.label.setPosition(self._label_pos + self._hover_label_pos_offset)
    else:
        self._label_pos = self.label.orthoPos - self._hover_label_pos_offset
        self.label.setFormat(self._default_label)
        self.label.setPosition(self._label_pos)
    self.update()


Furthermore, if the size of the plot item changes, then the location of the label also changes, since I cannot use `position=1` (do to the multiple lines).

This problem can be solved if I were able to align the top left corner of the label at `position` instead of the text item's vertical center.
How would I make that change? Or better yet, can this be added as a feature specified in labelOpts?

Patrick

unread,
Apr 11, 2019, 10:01:26 PM4/11/19
to pyqtgraph
Hi,

I think you want to change the anchor point of the label? In that case there is an anchor property of TextItems like the InfiniteLineLabel. Although it is not listed on the API documents for some reason (http://www.pyqtgraph.org/documentation/graphicsItems/textitem.html), there does appear to be a setAnchor() method in the code (http://www.pyqtgraph.org/documentation/_modules/pyqtgraph/graphicsItems/TextItem.html).
I think maybe a self.label.setAnchor(0, 0) is what you might be after.

Patrick
Message has been deleted
Message has been deleted

truef...@gmail.com

unread,
Apr 12, 2019, 12:52:05 PM4/12/19
to pyqtgraph
Oh, that's exactly what I am looking for. I had misinterpreted what it meant.
Thank you!

However, something else awkward is still happening (it was doing this before). Whenever the x-axis is zoomed far enough out, the label begins to get pushed up (beyond the maximum limit).
When my x-axis is set large enough relative to the y-axis (x values are changed from velocity to frequency while the y values remain the same), the label is placed above the visible range.

Patrick

unread,
Apr 14, 2019, 8:53:25 PM4/14/19
to pyqtgraph
Hi,

Are the zoom limits where things go wrong realistic? If not, then you could try restricting the zoom limits with something like plotitem.setLimits(minXRange=1.0, maxXRange=1e6)

Otherwise, is the problem because of a difference in plot vs pixel coordinates? That is, a setPosition() call places items on the plot using plot coordinates but, because a TextItem stays the same (pixel) size and is not scaled with the view, your _hover_label_pos_offset (measured in plot coordinates) becomes small/zero as you zoom out, and so isn't enough to shift the label down into the plot range.
If that's the case, then you might need to convert coordinate systems, eg using the getViewBox().mapFromView() method.

Patrick

truef...@gmail.com

unread,
Apr 16, 2019, 7:43:03 AM4/16/19
to pyqtgraph
I already have the zoom set with setLimits. Also, since the text is able to be aligned at the top of the TextItem using anchor, I no longer have included the _hover_label_pos_offset; thus I am not customizing or modifying the position of the label.
Reply all
Reply to author
Forward
0 new messages