This is a somewhat old question now- Hopefully this will help someone. Answering this helped me answer my own question.
Please note that I used PySide2 rather than PyQt4- I don't think this is significantly different to PyQt4. I am also using pyqtgraph 0.11.1.
There is a getLabel() method of the LegendItem that returns the LabelItem inside the legend for a given plotItem. This should allow you to do what you want.
You created your legend with this code:
legend = graph2.addLegend()style = pg.PlotDataItem(pen='w')legend.addItem(style, 'A2')
You can then get the labelitem with:
legend_labelitem = legend.getLabel(style)
With that you should be able to change the properties - such as using .setText() to set a new legend text:
legend_labelitem.setText('Something else')
The full code would end up as this:
import pyqtgraph as pg # from PySide2 import QtGui # <---- tested with thisfrom PyQt4 import QtGui import numpy as np import sys def main(): app = QtGui.QApplication(sys.argv) widg = QtGui.QWidget() widg.move(100, 100) pg.setConfigOption('background', 'w')pg.setConfigOption('foreground', 'k') pgWidg = pg.GraphicsLayoutWidget() pgWidg.resize(750, 250) graph1 = pgWidg.addPlot(row=1, col=1) graph2 = pgWidg.addPlot(row=1, col=2)curve1 = graph1.plot(y=np.sin(np.linspace(1, 21, 1000)), pen='k') curve2 = graph2.plot(y=np.sin(np.linspace(1, 21, 1000)), pen='k') graph1.addItem(curve1) graph2.addItem(curve2) graph1.setMouseEnabled(x=False, y=True)graph2.setMouseEnabled(x=False, y=True)graph1Text = pg.TextItem(text = 'A1', color=(0, 0, 0))graph1.addItem(graph1Text)graph1Text.setPos(150, 1)legend = graph2.addLegend()style = pg.PlotDataItem(pen='w')legend.addItem(style, 'A2')legend_labelitem = legend.getLabel(style) # <---------legend_labelitem.setText('Something else') # <---------grid = QtGui.QGridLayout() grid.addWidget(pgWidg, 0,0) widg.setLayout(grid) widg.show() sys.exit(app.exec_()) if __name__ == '__main__': main()
It produces this: