Correct way of updating text labels in pyqtgraph legend?

Hello,

I have an annoying problem when updating text entries of a legend created with pygtgraph / GraphicsLayoutWidget which is embedded in a QWidget. My idea is to create a couple of methods that should display additional information in the legend, like maximum or minimum values of plotted data. That works great but when I try to display my original label than some white spaces are added to the legend, see figure below. Do you have any ideas why white spaces appears even if the text string is correct, i.e. does not contain any white spaces?

import sys
import pyqtgraph as pg
import numpy as np
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QWidget
from PyQt5.QtWidgets import QHBoxLayout
from PyQt5.QtWidgets import QCheckBox
class Graph(QWidget):
    def __init__(self):
        super().__init__()
        self.win = pg.GraphicsLayoutWidget()
        self.plot = self.win.addPlot(0, 0)
        self.legend = self.plot.addLegend()
        self.data = np.linspace(0, 1, 50)
        self.plotLabel = 'Data1'
        self.plotDataItem = self.plot.plot(self.data, pen="r", name = self.plotLabel, antialias = True)
        self.labelItem = self.legend.getLabel(self.plotDataItem)
        self.checkbox_showMaxValue = QCheckBox('showMaxValue')
        self.checkbox_showMaxValue.stateChanged.connect(self.on_checkbox_showMaxValue_stateChanged)
        hbox = QHBoxLayout()
        hbox.addWidget(self.win)
        hbox.addWidget(self.checkbox_showMaxValue)
        self.setLayout(hbox)
    def changeText_labelIem1(self, text):
        self.labelItem1.setText(text)
    def changeText_labelIem2(self, text):
        self.labelItem2.setText(text)
    def on_checkbox_showMaxValue_stateChanged(self):
        try:
            if self.checkbox_showMaxValue.isChecked():
                self.labelItem.setText(self.plotLabel + ': ' + str(np.max(self.data)))
            else:
                self.labelItem.setText(self.plotLabel)
        except Exception as e:
            print(e)
if __name__ == '__main__':
    application = QApplication(sys.argv)
    graph = Graph()
    graph.show()
    sys.exit(application.exec_())

Best regards /Jonas

I found a simple solution for the problem. There was a built in method “self.legend.updateSize()” that removed the space between the text and the line icons.

Best regards /Jonas