Show text in the middle of a plot

701 views
Skip to first unread message

Jeffrey Nichols

unread,
Mar 25, 2021, 9:49:30 AM3/25/21
to pyqtgraph

I've got what I hope is a simple question here.

Background: I've got a pair of plots, and I can add axis labels using setLabel() and I can change the plot title using setTitle(). These work great.

I'd like to do something similar during error conditions and have the text right in the middle of the plot. Seems like it should be easy enough, as pyqtgraph is very flexible. I looked through the API and I didn't see anything quite right though. I don't want to attach it to a point in the graph, because usually there aren't points when I want to show the error. Ideally the message would be centered right in the middle of where the data would normally be displayed.

What's the easiest way to accomplish this?

Thanks,
Jeff

azpa...@gmail.com

unread,
Mar 25, 2021, 10:43:13 AM3/25/21
to pyqtgraph
Hi Jeff,
I think the text item example might fit your needs. 

Jeffrey Nichols

unread,
Mar 25, 2021, 11:32:00 AM3/25/21
to pyqtgraph
I'd seen that example, but it looked like it was attaching text at specific data coordinates, and the text moves with the data.

I wanted my text to be floating over the data (or more likely, floating over an absence of data) centered in the visible plot area, not attached to any specific point.

Looking at that example again, I noticed the comment in at the top of the file: "For text that can be placed in a layout, use LabelItem". I looked through the source code for LabelItem usage examples but I didn't see much.

Martin Chase

unread,
Mar 25, 2021, 5:26:40 PM3/25/21
to pyqt...@googlegroups.com
See examples/Legend.py, which uses the LegendItem (which itself uses LabelItem) to put text in a fixed position inside the plot.

Israel Brewster

unread,
Mar 25, 2021, 5:34:44 PM3/25/21
to pyqtgraph
Another possible approach - I’m not saying it’s a good one necessarily, just possible - is to recognize that all PyQtGraph objects are just standard Qt objects at their core, and simply create and position a QLabel object as desired, using standard Qt commands based on the position of your plot area.

Like I said, not necessarily a good approach, just spitballing possibilities :-)
---
Israel Brewster
Software Engineer
Alaska Volcano Observatory 
Geophysical Institute - UAF 
2156 Koyukuk Drive 
Fairbanks AK 99775-7320

-- 
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 pyqtgraph+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyqtgraph/b8feddee-ff70-4181-a14e-c15b15b8b724n%40googlegroups.com.

Jeffrey Nichols

unread,
Mar 26, 2021, 10:02:33 AM3/26/21
to pyqtgraph
Thanks for the tip. LegendItem was a bit awkward for my needs, but reading through its source code let me figure out how it worked (at least a little bit), and GraphicsWidgetAnchor subclass and anchor() was the secret I was missing. Turns out LabelItem already subclassed GraphicsWidgetAnchor, and it was easier to use than LegendItem for my use case. So all I needed to do was call anchor() on a LabelItem.

I threw this code in near the end of examples/Legend.py as a proof of concept and it did exactly what I wanted:

label = pg.LabelItem("Error", size="36pt", color="FF0000")
label.setParentItem(win.graphicsItem())
label.anchor(itemPos=(0.5,0.5), parentPos=(0.5,0.5))


Thanks for your help!
Jeff
Reply all
Reply to author
Forward
0 new messages