简体   繁体   中英

How to make a label inside a PlotWidget?

I'm making a GUI using Qt and pyqtgraph. It's got several PlotWidgets in it - here's a section of it:

图形用户界面

I'd like to include the mouse cursor position (in scaled units) as a label within the plot, a bit like the pyqtgraph crosshair example:

pygraph 十字准线示例

The example works by adding a LabelItem to a GraphicsWindow like this:

win = pg.GraphicsWindow()
label = pg.LabelItem(justify='right')
p1 = win.addPlot(row=1, col=0)

But I don't have a GraphicsWindow , just a normal Qt window (built using the Designer) with PlotWidgets in it. I can't seem to add a LabelItem or a TextItem to a PlotWidget . I'm sure there must be a 'standard' way of doing this, but I can't figure it out and Google doesn't seem to know. Any ideas?

Edit : here's a snippet of my code from the window's __init__ function:

self.B_field_plot.setLabels(title='Magnetic field', left='B [T]', bottom='z [m]')
self.label = pg.LabelItem(justify="right")
self.B_field_plot.addItem(self.label)
self.label.setText('Hello')

The label does not appear.

Although it is a bit late now to answer this question, but I will put my answer in case someone else needed it.

You have to change the source file generated by the QTdesigner. find the line that creates the plotwidget and embedded the title in it. self.plot = PlotWidget(title= "something")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM