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:
GraphicsWidgetThis 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. UseaddItem()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()- Parameters:
parent (
QObjectorNone, defaultNone) – ParentQObjectassign to thePlotItem.name (
strorNone, defaultNone) – Register the value for this view so that others may link to it.labels (
dictofstrorNone, defaultNone) – 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 (
ViewBoxorNone, defaultNone) – Have the PlotItem use the providedViewBox. If not specified, the PlotItem will be constructed with this as its ViewBox.axisItems (
dictof{'left', 'bottom', 'right', 'top }andAxisItemorNone) – Pass pre-constructedAxisItemobjects 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 toPlotItem.plot().
- Signals:
sigYRangeChanged (
Signal) – Signal is emitted when the range on the y-axis changes. Signal contains a reference to theViewBox, and tuple of (xmin, xmax).sigXRangeChanged (
Signal) – Signal is emitted when the range on the x-axis changes. Signal contains a reference to theViewBox, 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 theViewBox, 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 (
ImageItemorlistofImageItem) – SeesetImageItem()for details.**kwargs (
dict, optional) – Keyword arguments passed to theColorBarItemconstructor.
- Returns:
The newly created
ColorBarIteminstance.- Return type:
- addItem(
- item,
- *args,
- **kwargs,
Add a
GraphicsItemto theViewBox.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 toaddItem().**kwargs (
dict) – Keyword arguments for adding an item. Supported arguments include.Property
Description
ignoreBounds
bool- Keyword argument relayed toaddItem().skipAverage
bool- IfTrue, 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
LegendItemhas already been created using this method, that item will be returned rather than creating a new one.- Parameters:
offset (
tupleofint,int) – The distance to offset the LegendItem, defaults to(30, 30).**kwargs (
dict, optional) – Keyword argument passed to theLegendItemconstructor.
- Returns:
The instance of
LegendItemthat was constructed.- Return type:
- addLine(
- x=None,
- y=None,
- z=None,
- **kwargs,
Create a new
InfiniteLineand add it to the plot.- Parameters:
x (
floatorNone) – Position in the x-axis to draw the line. If specified, the line will be vertical. DefaultNone.y (
floatorNone) – Position in the y-axis to draw the line. If specified, the line will be horizontal. DefaultNone.z (
intorNone) – Z value to set the line to. This is used to determine which items are on top of the other. SeesetZValue().**kwargs (
dict) – Keyword arguments to pass to theInfiniteLineinstance.
- Returns:
The new
InfiniteLineadded.- Return type:
- clipToViewMode() bool[source]#
Return whether clip-to-view mode is enabled.
- Returns:
Trueif clip-to-view mode is enabled,Falseotherwise.- Return type:
bool
- hideAxis(
- axis,
Hide an axis.
- Parameters:
axis (
{'left', 'bottom', 'right', 'top'}) – The axis to hide.
- listDataItems()[source]#
Return a list of all data items current plotted.
- Returns:
A copy of a list of the data items.
- Return type:
listofPlotCurveItem,ScatterPlotItemorPlotDataItem
Return whether the context menu is enabled.
- Returns:
Trueif the context menu is enabled,Falseotherwise.- 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
ncurves, 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_nandy_narendarraydata for each curve. Sincexandyvalues are matched usingzip, 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 duringplot().**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
PlotDataIteminstances representing the new curves.- Return type:
listofPlotDataItem
- plot(
- *args,
- **kwargs,
Add and return a new
PlotDataItem.- Parameters:
*args (
tuple, optional) – Arguments that are passed to thePlotDataItemconstructor.**kwargs (
dict, optional) – Keyword arguments that are passed to thePlotDataItemconstructor. In addition, the following keyword arguments are accepted.
- Returns:
The newly created
PlotDataItem.- Return type:
- removeItem(
- item,
Remove an item from the plot.
- Parameters:
item (
GraphicsItem) – The item to remove.
- scatterPlot(
- *args,
- **kwargs,
Create a
PlotDataItemand add it to the plot.The
PlotDataIteminstance will render the underlying data in a scatter plot form.- Parameters:
*args (
tuple, optional) – Arguments that are passed to thePlotDataItemconstructor.**kwargs (
dict, optional) – Keyword arguments that are passed to thePlotDataItemconstructor. 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 tosymbolPen.brush
QBrush- Sets the brush used to fill symbols for the plot. Equivalent tosymbolBrush.size
float- Sets the symbol size for the plot. Equivalent tosymbolSize.
- Returns:
The newly created
PlotDataItem.- Return type:
- setAxisItems(
- axisItems: dict[str, AxisItem] | None = None,
Place axis items and initializes non-existing axis items.
- setClipToView(
- clip,
Set the default clip-to-view mode for new curves.
- Parameters:
clip (
bool) – IfTrue, new curves will be clipped to the view box.
- setContextMenuActionVisible(
- name: str,
- visible: bool,
Change the context menu action visibility.
- Parameters:
name (
{'Transforms', 'Downsample', 'Average', 'Alpha', 'Grid', 'Points'}) – Action name.visible (
bool) – Determines if action will be display.Trueaction is visible,Falseaction 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 (
intorboolorNone, optional) – The downsampling factor. IfNone, the downsampling factor is not changed. IfTrue, the downsampling factor is set to the value in the downsampling spin box. IfFalse, downsampling is disabled. If an integer, the downsampling factor is set to this value. Default isNone.auto (
boolorNone, optional) – IfTrue, automatic downsampling is enabled. IfFalse, automatic downsampling is disabled. IfNone, the automatic downsampling setting is not changed. Default isNone.mode (
{'subsample', 'mean', 'peak'}orNone, optional) – The downsampling mode. IfNone, the downsampling mode is not changed. Default isNone.
- 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) – IfTrue, the item will allow export.opts (
dictorNone, optional) – A dictionary of export options. IfNone, 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 whichAxisItemto set the label for.*args (
tuple, optional) – All extra arguments are passed tosetLabel().**kwargs (
dict, optional) – Keyword arguments are passed tosetLabel().
- setLabels(
- **kwargs,
Set the axis labels of the plot.
- Parameters:
**kwargs (
dict, optional) – Keyword arguments are passed tosetLabel(). The special keywordtitlecan be used to set the plot title usingsetTitle().
- 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 (
boolorNone) – IfTrue, set the x-axis to log mode. A value ofNoneis ignored.y (
boolorNone) – IfTrue, set the y-axis to log mode. A value ofNoneis ignored.
Notes
No other items, in the scene will be affected by this; there is (currently) no generic way to redisplay a
GraphicsItemwith 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=Noneto leave the ViewBox unchanged.- Parameters:
enableMenu (
bool, optional) – Whether to enable or disable the context menu, by default True.enableViewBoxMenu (
strorbool, optional) – Whether to enable or disable the ViewBox context menu. If ‘same’, the ViewBox menu will be enabled or disabled as enableMenu. IfTrueorFalse, the ViewBox menu will be set accordingly. IfNone, 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. IfNone, the title will be hidden. The default isNone.**args (
dict) – Additional keyword arguments are passed tosetText().
- showAxes(
- selection,
- showValues=True,
- size=False,
Convenience method for quickly configuring axis settings.
- Parameters:
selection (
boolortupleofbool) – 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 thatshowAxes(True)configures the axes to draw a frame.showValues (
boolortupleofbool, optional) – Determines if values will be displayed for the ticks of each axis.Truevalue shows values for left and bottom axis (default).Falseshows no values. If in tuple form, order is (left, top, right, bottom).Noneleaves settings unchanged. If not specified, left and bottom axes will be drawn with values.size (
floatortupleoffloat, 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. IfNoneis 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.
- showGrid(
- x=None,
- y=None,
- alpha=None,
Show or hide the grid for either axis.
- Parameters:
x (
boolorNone) – Show the X grid, a value ofNoneis ignored.y (
boolorNone) – Show the Y grid, a value ofNoneis ignored.alpha (
floatorNone) – 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[source]#
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.