LegendItem

class pyqtgraph.LegendItem(size=None, offset=None, horSpacing=25, verSpacing=0, pen=None, brush=None, labelTextColor=None, **kwargs)[source]

Displays a legend used for describing the contents of a plot. LegendItems are most commonly created by calling PlotItem.addLegend().

Note that this item should not be added directly to a PlotItem. Instead, Make it a direct descendant of the PlotItem:

legend.setParentItem(plotItem)
__init__(size=None, offset=None, horSpacing=25, verSpacing=0, pen=None, brush=None, labelTextColor=None, **kwargs)[source]

Arguments:

size

Specifies the fixed size (width, height) of the legend. If this argument is omitted, the legend will automatically resize to fit its contents.

offset

Specifies the offset position relative to the legend’s parent. Positive values offset from the left or top; negative values offset from the right or bottom. If offset is None, the legend must be anchored manually by calling anchor() or positioned by calling setPos().

horSpacing

Specifies the spacing between the line symbol and the label.

verSpacing

Specifies the spacing between individual entries of the legend vertically. (Can also be negative to have them really close)

pen

Pen to use when drawing legend border. Any single argument accepted by mkPen is allowed.

brush

QBrush to use as legend background filling. Any single argument accepted by mkBrush is allowed.

labelTextColor

Pen to use when drawing legend text. Any single argument accepted by mkPen is allowed.

addItem(item, name)[source]

Add a new entry to the legend.

Arguments:

item

A PlotDataItem from which the line and point style of the item will be determined or an instance of ItemSample (or a subclass), allowing the item display to be customized.

title

The title to display for this item. Simple HTML allowed.

boundingRect(self) → PySide2.QtCore.QRectF[source]
clear()[source]

Removes all items from the legend.

Useful for reusing and dynamically updating charts and their legends.

paint(self, painter: PySide2.QtGui.QPainter, option: PySide2.QtWidgets.QStyleOptionGraphicsItem, widget: typing.Union[PySide2.QtWidgets.QWidget, NoneType] = None)[source]
removeItem(item)[source]

Removes one item from the legend.

Arguments:

item

The item to remove or its name.

setLabelTextColor(*args, **kargs)[source]

Sets the color of the label text. pen can be a QPen or any argument accepted by pyqtgraph.mkColor()

setParentItem(self, parent: PySide2.QtWidgets.QGraphicsItem)[source]
setPen(*args, **kargs)[source]

Sets the pen used to draw lines between points. pen can be a QPen or any argument accepted by pyqtgraph.mkPen()