PlotItem#

class pyqtgraph.PlotItem[source]#

GraphicsWidget implementing a standard 2D plotting area with axes.

Bases: GraphicsWidget

This class provides the ViewBox-plus-axes that appear when using pg.plot(), PlotWidget, and GraphicsLayout.addPlot().

It’s main functionality is:

  • Manage placement of ViewBox, AxisItems, and LabelItems

  • Create and manage a list of PlotDataItems displayed inside the ViewBox

  • Implement a context menu with commonly used display and analysis options

Use plot() to create a new PlotDataItem and add it to the view. Use addItem() to add any QGraphicsItem to the view.

This class wraps several methods from its internal ViewBox:

The ViewBox itself can be accessed by calling getViewBox()

Signals:

sigYRangeChanged

wrapped from ViewBox

sigXRangeChanged

wrapped from ViewBox

sigRangeChanged

wrapped from ViewBox

__init__(parent=None, name=None, labels=None, title=None, viewBox=None, axisItems=None, enableMenu=True, **kargs)[source]#

Create a new PlotItem. All arguments are optional. Any extra keyword arguments are passed to PlotItem.plot().

Arguments:

title

Title to display at the top of the item. Html is allowed.

labels

A dictionary specifying the axis labels to display:

{'left': (args), 'bottom': (args), ...}

The name of each axis and the corresponding arguments are passed to PlotItem.setLabel() Optionally, PlotItem my also be initialized with the keyword arguments left, right, top, or bottom to achieve the same effect.

name

Registers a name for this view so that others may link to it

viewBox

If specified, the PlotItem will be constructed with this as its ViewBox.

axisItems

Optional dictionary instructing the PlotItem to use pre-constructed items for its axes. The dict keys must be axis names (‘left’, ‘bottom’, ‘right’, ‘top’) and the values must be instances of AxisItem (or at least compatible with AxisItem).

addColorBar(image, **kargs)[source]#

Adds a color bar linked to the ImageItem specified by image. AAdditional parameters will be passed to the pyqtgraph.ColorBarItem.

A call like plot.addColorBar(img, colorMap=’viridis’) is a convenient method to assign and show a color map.

addItem(item, *args, **kargs)[source]#

Add a graphics item to the view box. If the item has plot data (PlotDataItem , PlotCurveItem , ScatterPlotItem ), it may be included in analysis performed by the PlotItem.

addLegend(offset=(30, 30), **kwargs)[source]#

Create a new LegendItem and anchor it over the internal ViewBox. Plots added after this will be automatically displayed in the legend if they are created with a ‘name’ argument.

If a LegendItem has already been created using this method, that item will be returned rather than creating a new one.

Accepts the same arguments as __init__().

addLine(x=None, y=None, z=None, **kwds)[source]#

Create an InfiniteLine and add to the plot.

If x is specified, the line will be vertical. If y is specified, the line will be horizontal. All extra keyword arguments are passed to InfiniteLine.__init__(). Returns the item created.

clear()[source]#

Remove all items from the PlotItem’s ViewBox.

getAxis(name)[source]#

Return the specified AxisItem. name should be ‘left’, ‘bottom’, ‘top’, or ‘right’.

getViewBox()[source]#

Return the ViewBox contained within.

hideAxis(axis)[source]#

Hide one of the PlotItem’s axes. (‘left’, ‘bottom’, ‘right’, or ‘top’)

hideButtons()[source]#

Causes auto-scale button (‘A’ in lower-left corner) to be hidden for this PlotItem

listDataItems()[source]#

Return a list of all data items (PlotDataItem, PlotCurveItem , ScatterPlotItem , etc) contained in this PlotItem.

multiDataPlot(*, x=None, y=None, constKwargs=None, **kwargs)[source]#

Allow plotting multiple curves on the same plot, changing some kwargs per curve.

Parameters:
  • x (array_like) –

    can be in the following formats:
    • {x or y} = [n1, n2, n3, …]: The named argument iterates through n curves, while the unspecified argument is range(len(n)) for each curve.

    • x, [y1, y2, y3, …]

    • [x1, x2, x3, …], [y1, y2, y3, …]

    • [x1, x2, x3, …], y

    where x_n and y_n are ndarray data for each curve. Since x and y values are matched using zip, unequal lengths mean the longer array will be truncated. Note that 2D matrices for either x or y are considered lists of curve data.

  • y (array_like) –

    can be in the following formats:
    • {x or y} = [n1, n2, n3, …]: The named argument iterates through n curves, while the unspecified argument is range(len(n)) for each curve.

    • x, [y1, y2, y3, …]

    • [x1, x2, x3, …], [y1, y2, y3, …]

    • [x1, x2, x3, …], y

    where x_n and y_n are ndarray data for each curve. Since x and y values are matched using zip, unequal lengths mean the longer array will be truncated. Note that 2D matrices for either x or y are considered lists of curve data.

  • constKwargs (dict, optional) – A dict of {str: value} passed to each curve during plot().

  • kwargs (dict, optional) – A dict of {str: iterable} where the str is the name of a kwarg and the iterable is a list of values, one for each plotted curve.

plot(*args, **kargs)[source]#

Add and return a new plot. See PlotDataItem.__init__ for data arguments

Additional allowed arguments

clear

clears all plots before displaying new data

params

sets meta-parameters to associate with this data

removeItem(item)[source]#

Remove an item from the PlotItem’s ViewBox.

setAxisItems(axisItems=None)[source]#

Place axis items as given by axisItems. Initializes non-existing axis items.

Arguments:

axisItems

Optional dictionary instructing the PlotItem to use pre-constructed items for its axes. The dict keys must be axis names (‘left’, ‘bottom’, ‘right’, ‘top’) and the values must be instances of AxisItem (or at least compatible with AxisItem).

setClipToView(clip)[source]#

Set the default clip-to-view mode for all PlotDataItem s managed by this plot. If clip is True, then PlotDataItems will attempt to draw only points within the visible range of the ViewBox.

setContextMenuActionVisible(name, visible)[source]#

Changes the context menu action visibility

Parameters:
  • name (str) – Action name must be one of ‘Transforms’, ‘Downsample’, ‘Average’,’Alpha’, ‘Grid’, or ‘Points’

  • visible (bool) – Determines if action will be display True action is visible False action is invisible.

Return type:

None

setDownsampling(ds=None, auto=None, mode=None)[source]#

Changes the default downsampling mode for all PlotDataItem managed by this plot.

Arguments:

ds

(int) Reduce visible plot samples by this factor, or

(bool) To enable/disable downsampling without changing the value.

auto

(bool) If True, automatically pick ds based on visible range

mode

‘subsample’: Downsample by taking the first of N samples. This method is fastest but least accurate.

‘mean’: Downsample by taking the mean of N samples.

‘peak’: Downsample by drawing a saw wave that follows the min and max of the original data. This method produces the best visual representation of the data but is slower.

setLabel(axis, text=None, units=None, unitPrefix=None, **args)[source]#

Sets the label for an axis. Basic HTML formatting is allowed.

Arguments:

axis

must be one of ‘left’, ‘bottom’, ‘right’, or ‘top’

text

text to display along the axis. HTML allowed.

units

units to display after the title. If units are given, then an SI prefix will be automatically appended and the axis values will be scaled accordingly. (ie, use ‘V’ instead of ‘mV’; ‘m’ will be added automatically)

setLabels(**kwds)[source]#

Convenience function allowing multiple labels and/or title to be set in one call. Keyword arguments can be ‘title’, ‘left’, ‘bottom’, ‘right’, or ‘top’. Values may be strings or a tuple of arguments to pass to setLabel().

setLogMode(x=None, y=None)[source]#

Set log scaling for x and/or y axes. This informs PlotDataItems to transform logarithmically and switches the axes to use log ticking.

Note that no other items in the scene will be affected by this; there is (currently) no generic way to redisplay a GraphicsItem with log coordinates.

setMenuEnabled(enableMenu=True, enableViewBoxMenu='same')[source]#

Enable or disable the context menu for this PlotItem. By default, the ViewBox’s context menu will also be affected. (use enableViewBoxMenu=None to leave the ViewBox unchanged)

setTitle(title=None, **args)[source]#

Set the title of the plot. Basic HTML formatting is allowed. If title is None, then the title will be hidden.

showAxes(selection, showValues=True, size=False)[source]#

Convenience method for quickly configuring axis settings.

Parameters:
  • selection (bool or tuple of bool) – Determines which AxisItems will be displayed. If in tuple form, order is (left, top, right, bottom) A single boolean value will set all axes, so that showAxes(True) configures the axes to draw a frame.

  • showValues (bool or tuple of bool, optional) – Determines if values will be displayed for the ticks of each axis. True value shows values for left and bottom axis (default). False shows no values. If in tuple form, order is (left, top, right, bottom) None leaves settings unchanged. If not specified, left and bottom axes will be drawn with values.

  • size (float or tuple of float, optional) – Reserves as fixed amount of space (width for vertical axis, height for horizontal axis) for each axis where tick values are enabled. If only a single float value is given, it will be applied for both width and height. If None is given instead of a float value, the axis reverts to automatic allocation of space. If in tuple form, order is (width, height)

showAxis(axis, show=True)[source]#

Show or hide one of the plot’s axes. axis must be one of ‘left’, ‘bottom’, ‘right’, or ‘top’

showButtons()[source]#

Causes auto-scale button (‘A’ in lower-left corner) to be visible for this PlotItem

showGrid(x=None, y=None, alpha=None)[source]#

Show or hide the grid for either axis.

Arguments:

x

(bool) Whether to show the X grid

y

(bool) Whether to show the Y grid

alpha

(0.0-1.0) Opacity of the grid

showLabel(axis, show=True)[source]#

Show or hide one of the plot’s axis labels (the axis itself will be unaffected). axis must be one of ‘left’, ‘bottom’, ‘right’, or ‘top’

updateDecimation()[source]#

Reduce or increase number of visible curves according to value set by the Max Traces spinner, if Max Traces is checked in the context menu. Destroy curves that are not visible if forget traces is checked. In most cases, this function is called automaticaly when the Max Traces GUI elements are triggered. It is also alled when the state of PlotItem is updated, its state is restored, or new items added added/removed.

This can cause an unexpected or conflicting state of curve visibility (or destruction) if curve visibilities are controlled externally. In the case of external control it is advised to disable the Max Traces checkbox (or context menu) to prevent unexpected curve state changes.

viewGeometry()[source]#

Return the screen geometry of the viewbox