How to make a label inside a Plot Widgets?

167 views
Skip to first unread message

Hamza Khan

unread,
May 3, 2021, 5:51:49 AM5/3/21
to pyqtgraph
Hi All, I am a new pyqtgraph users trying to make a label inside a Plot Widgets.

Here is my code:

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import numpy as np
import pyqtgraph as pg
from PyQt4 import QtCore

class PlotDashboard(QWidget):
    def __init__(self, title, parent=None):
        '''Constructor
        '''        
        QWidget.__init__(self, parent)

        self.layout = QVBoxLayout(self)
        self.setLayout(self.layout)
        self.label = QLabel(self)

        # Create a plot with a date-time axis
        self.p1 = pg.PlotWidget(name='Plot1', background=QColor(5, 27, 105, 255), axisItems = {'bottom': pg.DateAxisItem()})  ## giving the plots names allows us to link their axes together
        
        self.p1.setTitle("<strong>" + title + "</strong>")
        self.p1.getAxis("left").setPen(pg.mkPen(color='w', width=2))
        self.p1.getAxis("bottom").setPen(pg.mkPen(color='w', width=2))
        
        self.p1.showGrid(x=True, y=True, alpha = 0.3)
        self.p1.setLabel('left', 'Value')
        self.p1.setLabel('bottom', 'Time')
        self.p1.addLegend()
                                      
        # Add layout
        self.layout.addWidget(self.label)
        self.label.setAlignment(QtCore.Qt.AlignRight)
        self.layout.addWidget(self.p1)
        
        self.btnClear = QPushButton(text = 'Clear All')
        self.btnClear.setIcon(QIcon(QPixmap('/usr/local/lib/python2.7/dist-packages/taurus/qt/qtgui/icon/Tango/scalable/actions/editclear.svg')))
        self.btnClear.clicked.connect(self.clearAll)
        self.layout.addWidget(self.btnClear)
        
        # Data holders for cross hair- Plot max 5 data sets
        self.data1 = [[], []] # x and y in separate lists
        self.data2 = [[], []]
        self.data3 = [[], []]
        self.data4 = [[], []]
        self.data5 = [[], []]
        
        self.curvesData = [self.data1, self.data2, self.data3, self.data4, self.data5]
        self.numCurves = 0 # number of curves plotted, runs from 0 to 3

        vLine = pg.InfiniteLine(angle=90, movable=False)
        hLine = pg.InfiniteLine(angle=0, movable=False)
        self.p1.addItem(vLine, ignoreBounds=True)
        self.p1.addItem(hLine, ignoreBounds=True)

        vb = self.p1.getViewBox()

        def mouseMoved(evt):
            pos = evt[0]
            if self.p1.sceneBoundingRect().contains(pos):
                mousePoint = vb.mapSceneToView(pos)
                self.label.setText("x: %d \t y: %d" % (mousePoint.x(), mousePoint.y()))
                vLine.setPos(mousePoint.x())
                hLine.setPos(mousePoint.y())


        self.p1.getViewBox().setAutoVisible(y=True)
        proxy = pg.SignalProxy(self.p1.scene().sigMouseMoved, rateLimit=60, slot=mouseMoved)
        self.p1.proxy = proxy


    def plotCurve(self, data, curve_name):
        if len(data[0]) == 0 or len(data[1]) == 0: return
        if len(data[0]) != len(data[1]): return
        if self.numCurves > 4: return # support plotting max 5 curves, need to clear all first

        self.curvesData[self.numCurves] = data
        self.p1.addLegend()

        if self.numCurves == 0:
            _pen=(255,0,255)
            self.data1 = [data[0], data[1]]
        elif self.numCurves == 1:
            _pen=(0,255,0)
            self.data2 = [data[0], data[1]]
        elif self.numCurves == 2:
            _pen=(255,100,0)
            self.data3 = [data[0], data[1]]
        elif self.numCurves == 3:
            _pen=(255,255,255)
            self.data4 = [data[0], data[1]]
        else:
            _pen=(255,255,0)
            self.data5 = [data[0], data[1]]

        self.p1.plot(data[0], data[1], pen=_pen, symbolBrush=_pen, symbolPen='w', symbol='o', symbolSize=14, name=curve_name)
        self.numCurves += 1

    def clearAll(self):
        self.p1.clear()
        self.label.clear()
        self.data1 = [[], []]
        self.data2 = [[], []]
        self.data3 = [[], []]
        self.data4 = [[], []]
        self.data5 = [[], []]
        
        self.curvesData = [self.data1, self.data2, self.data3, self.data4, self.data5]
        self.numCurves = 0


if __name__ == "__main__":
    app = QApplication(sys.argv)
    gui = PlotDashboard("PLOT 1")
    gui.show()
    sys.exit(app.exec_())



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?

Carlos Pascual-Izarra

unread,
May 3, 2021, 10:29:56 AM5/3/21
to pyqt...@googlegroups.com
Maybe you are looking for TextItem  instead of LabelItem?

See:

https://github.com/pyqtgraph/pyqtgraph/blob/master/examples/text.py

Note: you can check this and other examples with:

`pyhton -m pyqtgraph.examples`

I hope this helps


--
+----------------------------------------------------+
Carlos Pascual Izarra
Scientific Software Coordinator
Computing Division
ALBA Synchrotron [http://www.albasynchrotron.es]
Carrer de la Llum 2-26
E-08290 Cerdanyola del Valles (Barcelona), Spain
E-mail: cpas...@cells.es
Phone: +34 93 592 4428
+----------------------------------------------------+

Reply all
Reply to author
Forward
0 new messages