Skip to main content
`Ctrl`+`K`

# PlotItem

class pyqtgraph.PlotItem(

  parent: QObject | None = None,
  name: str | None = None,
  labels: dict[str, str] | None = None,
  title: str | None = None,
  viewBox: ViewBox | None = None,
  axisItems: dict[str, AxisItem] | None = None,
  enableMenu: bool = True,
  **kwargs,

)

  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 :meth :GraphicsLayout.addPlot() <pyqtgraph.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:

    - `setXRange`

    - `setYRange`

    - `setRange`

    - `autoRange`

    - `setDefaultPadding`

    - `setXLink`

    - `setYLink`

    - `setAutoPan`

    - `setAutoVisible`

    - `setLimits`

    - `viewRect`

    - `viewRange`

    - `setMouseEnabled`

    - `enableAutoRange`

    - `disableAutoRange`

    - `setAspectLocked`

    - `invertY`

    - `invertX`

    - `register`

    - `unregister`

  The ViewBox itself can be accessed by calling `getViewBox()`

  Parameters:

    - parent (`QObject` or `None`, default `None`) – Parent `QObject` assign to the `PlotItem`.

    - name (`str` or `None`, default `None`) – Register the value for this view so that others may link to it.

    - labels (`dict` of `str` or `None`, default `None`) – 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.

    - title (`str`) – Text to set the title of the PlotItem to.

    - viewBox (`ViewBox` or `None`, default `None`) – Have the PlotItem use the provided `ViewBox`. If not specified, the PlotItem will be constructed with this as its ViewBox.

    - axisItems (`dict` of `{'left', 'bottom', 'right', 'top }` and `AxisItem` or `None`) – Pass pre-constructed `AxisItem` objects to be used for the left, bottom, right or top axis. Default is None.

    - enableMenu (`bool`) – Toggle the enabling or disabling of the right-click context menu.

    - **kwargs (`dict`, optional) – Any extra keyword arguments are passed to `PlotItem.plot()`.

  Signals:

    - sigYRangeChanged (`Signal`) – Signal is emitted when the range on the y-axis changes. Signal contains a reference to the `ViewBox`, and tuple of (xmin, xmax).

    - sigXRangeChanged (`Signal`) – Signal is emitted when the range on the x-axis changes. Signal contains a reference to the `ViewBox`, and tuple of (ymin, ymax).

    - sigRangeChanged (`Signal`) – Signal is emitted when the range on either x or y-axis changes. Signal contains a reference to the `ViewBox`, a list of lists of the form, `[[x_min, x_max], [y_min, y_max]]` and a list of two booleans, indicating if the range on which axis has changed of the form `[x_range_changed, y_range_changed]`.

  __init__(
    parent: QObject | None = None,
    name: str | None = None,
    labels: dict[str, str] | None = None,
    title: str | None = None,
    viewBox: ViewBox | None = None,
    axisItems: dict[str, AxisItem] | None = None,
    enableMenu: bool = True,
    **kwargs,
  )
  addColorBar(
    image,
    **kwargs,
  )

    Add a ColorBarItem and set to the provided image.

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

    Parameters:

      - image (`ImageItem` or `list` of `ImageItem`) – See `setImageItem()` for details.

      - **kwargs (`dict`, optional) – Keyword arguments passed to the `ColorBarItem` constructor.

    Returns:

      The newly created `ColorBarItem` instance.

    Return type:

      `ColorBarItem`

  addItem(
    item,
    *args,
    **kwargs,
  )

    Add a `GraphicsItem` to the `ViewBox`.

    If the item has plot data (`PlotDataItem` , `PlotCurveItem`, `ScatterPlotItem` ), it may be included in analysis performed by the PlotItem.

    Parameters:

      - item (`GraphicsItem`) – Item to add to the ViewBox.

      - *args (`tuple`) – Arguments relayed to `addItem()`.

      - **kwargs (`dict`) – Keyword arguments for adding an item. Supported arguments include.

        Property | Description
        --- | ---
        ignoreBounds | `bool` - Keyword argument relayed to `addItem()`.
        skipAverage | `bool` - If `True`, do not use curve to compute average curves.

    See also

    `addItem()`

      See method for supported arguments to be passed.

  addLegend(
    offset=(30, 30),
    **kwargs,
  )

    Add a new `LegendItem`.

    After the LegendItem is created, it is anchored in 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.

    Parameters:

      - offset (`tuple` of `int`, `int`) – The distance to offset the LegendItem, defaults to `(30, 30)`.

      - **kwargs (`dict`, optional) – Keyword argument passed to the `LegendItem` constructor.

    Returns:

      The instance of `LegendItem` that was constructed.

    Return type:

      `LegendItem`

  addLine(
    x=None,
    y=None,
    z=None,
    **kwargs,
  )

    Create a new `InfiniteLine` and add it to the plot.

    Parameters:

      - x (`float` or `None`) – Position in the x-axis to draw the line. If specified, the line will be vertical. Default `None`.

      - y (`float` or `None`) – Position in the y-axis to draw the line. If specified, the line will be horizontal. Default `None`.

      - z (`int` or `None`) – Z value to set the line to. This is used to determine which items are on top of the other. See `setZValue()`.

      - **kwargs (`dict`) – Keyword arguments to pass to the `InfiniteLine` instance.

    Returns:

      The new `InfiniteLine` added.

    Return type:

      `InfiniteLine`

  clear()

    Remove all items from the PlotItem’s `ViewBox`.

  clearPlots()

    Remove all curves from the PlotItem’s `ViewBox`.

  clipToViewMode() → bool

    Return whether clip-to-view mode is enabled.

    Returns:

      `True` if clip-to-view mode is enabled, `False` otherwise.

    Return type:

      `bool`

  getAxis(
    name,
  )

    Return the specified AxisItem.

    Parameters:

      name (`{'left', 'bottom', 'right', 'top'}`) – The name of the axis to return.

    Returns:

      The `AxisItem`.

    Return type:

      `AxisItem`

    Raises:

      KeyError – If the specified axis is not present.

  getViewBox() → ViewBox

    Return the embedded `ViewBox`.

    Returns:

      The `ViewBox` that this PlotItem uses.

    Return type:

      `ViewBox`

  hideAxis(
    axis,
  )

    Hide an axis.

    Parameters:

      axis (`{'left', 'bottom', 'right', 'top'}`) – The axis to hide.

  hideButtons()

    Hide auto-scale button (‘A’ in lower-left corner).

  listDataItems()

    Return a list of all data items current plotted.

    Returns:

      A copy of a list of the data items.

    Return type:

      `list` of `PlotCurveItem`, `ScatterPlotItem` or `PlotDataItem`

  menuEnabled()

    Return whether the context menu is enabled.

    Returns:

      `True` if the context menu is enabled, `False` otherwise.

    Return type:

      `bool`

  multiDataPlot(
    *,
    x=None,
    y=None,
    constKwargs=None,
    **kwargs,
  )

    Allow plotting multiple curves on the same plot in one call.

    Parameters:

      - x, 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.

    Returns:

      Returns a list of the newly constructed `PlotDataItem` instances representing the new curves.

    Return type:

      `list` of `PlotDataItem`

  plot(
    *args,
    **kwargs,
  )

    Add and return a new `PlotDataItem`.

    Parameters:

      - *args (`tuple`, optional) – Arguments that are passed to the `PlotDataItem` constructor.

      - **kwargs (`dict`, optional) – Keyword arguments that are passed to the `PlotDataItem` constructor. In addition, the following keyword arguments are accepted.

        Property | Description
        --- | ---
        clear | `bool` - Call `clear()` prior to creating the new plot instance.
        params | `dict` or `None` - Arguments to passed as the params argument to `addItem()`.

    Returns:

      The newly created `PlotDataItem`.

    Return type:

      `PlotDataItem`

  removeItem(
    item,
  )

    Remove an item from the plot.

    Parameters:

      item (`GraphicsItem`) – The item to remove.

  scatterPlot(
    *args,
    **kwargs,
  )

    Create a `PlotDataItem` and add it to the plot.

    The `PlotDataItem` instance will render the underlying data in a scatter plot form.

    Parameters:

      - *args (`tuple`, optional) – Arguments that are passed to the `PlotDataItem` constructor.

      - **kwargs (`dict`, optional) – Keyword arguments that are passed to the `PlotDataItem` constructor. In addition, the following keyword arguments are accepted.

        Property | Description
        --- | ---
        pen | `QPen` - Sets the pen used to draw lines or symbols for the plot. Equivalent to `symbolPen`.
        brush | `QBrush` - Sets the brush used to fill symbols for the plot. Equivalent to `symbolBrush`.
        size | `float` - Sets the symbol size for the plot. Equivalent to `symbolSize`.

    Returns:

      The newly created `PlotDataItem`.

    Return type:

      `PlotDataItem`

  setAxisItems(
    axisItems: dict[str, AxisItem] | None = None,
  ) → None

    Place axis items and initializes non-existing axis items.

    Parameters:

      axisItems (`dict` of `str` and `AxisItem` or `None`) – 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`.

    Return type:

      `None`

  setClipToView(
    clip,
  )

    Set the default clip-to-view mode for new curves.

    Parameters:

      clip (`bool`) – If `True`, new curves will be clipped to the view box.

  setContextMenuActionVisible(
    name: str,
    visible: bool,
  ) → None

    Change the context menu action visibility.

    Parameters:

      - name (`{'Transforms', 'Downsample', 'Average', 'Alpha', 'Grid', 'Points'}`) – Action name.

      - 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,
  )

    Set the downsampling mode for the PlotItem.

    Downsampling reduces the number of samples drawn to improve performance. The downsampling mode can be set to ‘subsample’, ‘mean’, ‘peak’, or None. If downsampling is enabled, the view will display downsampled data when zoomed out, but will display original data at high zoom levels.

    Parameters:

      - ds (`int` or `bool` or `None`, optional) – The downsampling factor. If `None`, the downsampling factor is not changed. If `True`, the downsampling factor is set to the value in the downsampling spin box. If `False`, downsampling is disabled. If an integer, the downsampling factor is set to this value. Default is `None`.

      - auto (`bool` or `None`, optional) – If `True`, automatic downsampling is enabled. If `False`, automatic downsampling is disabled. If `None`, the automatic downsampling setting is not changed. Default is `None`.

      - mode (`{'subsample', 'mean', 'peak'}` or `None`, optional) – The downsampling mode. If `None`, the downsampling mode is not changed. Default is `None`.

    Raises:

      ValueError – Raised if the specified downsample mode is not recognized.

  setExportMode(
    export: bool,
    opts=None,
  )

    Set whether the item will allow export via screenshots or image files.

    If export mode is enabled, then the item will allow export and the export options dock will be displayed. If export mode is disabled, then no export options will be available and the export dock will be hidden.

    Parameters:

      - export (`bool`) – If `True`, the item will allow export.

      - opts (`dict` or `None`, optional) – A dictionary of export options. If `None`, the default export options will be used. By default, `None`.

  setLabel(
    axis: str,
    *args,
    **kwargs,
  )

    Set the label for an axis.

    Basic HTML is allowed. See `AxisItem.setLabel()` for formatting options.

    Parameters:

      - axis (`{'left', 'bottom', 'right', 'top'}`) – Specify which `AxisItem` to set the label for.

      - *args (`tuple`, optional) – All extra arguments are passed to `setLabel()`.

      - **kwargs (`dict`, optional) – Keyword arguments are passed to `setLabel()`.

  setLabels(
    **kwargs,
  )

    Set the axis labels of the plot.

    Parameters:

      **kwargs (`dict`, optional) – Keyword arguments are passed to `setLabel()`. The special keyword `title` can be used to set the plot title using `setTitle()`.

  setLogMode(
    x=None,
    y=None,
  )

    Set log scaling for x and/or y axes.

    This informs PlotDataItems to transform logarithmically and switches the axes to use log ticking.

    Parameters:

      - x (`bool` or `None`) – If `True`, set the x-axis to log mode. A value of `None` is ignored.

      - y (`bool` or `None`) – If `True`, set the y-axis to log mode. A value of `None` is ignored.

    Notes

    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',
  )

    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.

    Parameters:

      - enableMenu (`bool`, optional) – Whether to enable or disable the context menu, by default True.

      - enableViewBoxMenu (`str` or `bool`, optional) – Whether to enable or disable the ViewBox context menu. If ‘same’, the ViewBox menu will be enabled or disabled as enableMenu. If `True` or `False`, the ViewBox menu will be set accordingly. If `None`, the ViewBox menu will not be changed. By default ‘same’.

  setTitle(
    title=None,
    **args,
  )

    Set the title of the plot.

    Parameters:

      - title (`str`, optional) – The title text. If `None`, the title will be hidden. The default is `None`.

      - **args (`dict`) – Additional keyword arguments are passed to `setText()`.

  showAxes(
    selection,
    showValues=True,
    size=False,
  )

    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: str,
    show: bool = True,
  )

    Show or hide an axis.

    Parameters:

      - axis (`{'left', 'bottom', 'right', 'top'}`) – The axis to show or hide.

      - show (`bool`, optional) – Whether to show or hide the axis, by default True.

  showButtons()

    Show auto-scale button (‘A’ in lower-left corner).

  showGrid(
    x=None,
    y=None,
    alpha=None,
  )

    Show or hide the grid for either axis.

    Parameters:

      - x (`bool` or `None`) – Show the X grid, a value of `None` is ignored.

      - y (`bool` or `None`) – Show the Y grid, a value of `None` is ignored.

      - alpha (`float` or `None`) – Opacity of the grid, float values need to be between [0.0, 1.0].

  showLabel(
    axis,
    show=True,
  )

    Show or hide one of the plot’s axis labels.

    Parameters:

      - axis (`{'left', 'bottom', 'right', 'top'}`) – The axis label to show or hide.

      - show (`bool`, optional) – Whether to show or hide the label, by default True.

  viewGeometry() → QRectF

    Return the screen geometry of the viewbox.

    Returns:

      The QRectF instance that contains the view area.

    Return type:

      `QRectF`

  writeCsv(
    fileName=None,
  )

    Write the plot data to a CSV file.

    Parameters:

      fileName (`str`, optional) – The name of the file to write to. If not specified, a file dialog will be opened.

  writeImage(
    fileName=None,
  )

    Write the plot content to an image file.

    Parameters:

      fileName (`str`, optional) – The name of the file to write to. If not specified, a file dialog will be opened.

  writeSvg(
    fileName=None,
  )

    Write the plot content to an SVG file.

    Parameters:

      fileName (`str`, optional) – The name of the file to write to. If not specified, a file dialog will be opened.