LegendItem

class pyqtgraph.LegendItem(size=None, offset=None, horSpacing=25, verSpacing=0, pen=None, brush=None, labelTextColor=None, frame=True, labelTextSize='9pt', colCount=1, sampleType=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 (via PlotItem.addItem). 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, frame=True, labelTextSize='9pt', colCount=1, sampleType=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.

labelTextSize

Size to use when drawing legend text. Accepts CSS style string arguments, e.g. ‘9pt’.

colCount

Specifies the integer number of columns that the legend should be divided into. The number of rows will be calculated based on this argument. This is useful for plots with many curves displayed simultaneously. Default: 1 column.

sampleType

Customizes the item sample class of the LegendItem.

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.

brush()[source]

Get the QBrush used to draw the legend background.

clear()[source]

Remove all items from the legend.

getLabel(plotItem)[source]

Return the labelItem inside the legend for a given plotItem

The label-text can be changed via labelItem.setText

labelTextColor()[source]

Get the QColor used for the item labels.

labelTextSize()[source]

Get the labelTextSize used for the item labels.

offset()[source]

Get the offset position relative to the parent.

pen()[source]

Get the QPen used to draw the border around the legend.

removeItem(item)[source]

Removes one item from the legend.

Arguments:

item

The item to remove or its name.

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

Set the brush used to draw the legend background.

Accepts the same arguments as mkBrush().

setColumnCount(columnCount)[source]

change the orientation of all items of the legend

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

Set the color of the item labels.

Accepts the same arguments as mkColor().

setLabelTextSize(size)[source]

Set the size of the item labels.

Accepts the CSS style string arguments, e.g. ‘8pt’.

setOffset(offset)[source]

Set the offset position relative to the parent.

setParentItem(p)[source]

Set the parent.

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

Set the pen used to draw a border around the legend.

Accepts the same arguments as mkPen().

setSampleType(sample)[source]

Set the new sample item claspes