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. 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 (
QObject
orNone
, defaultNone
) – ParentQObject
assign to thePlotItem
.name (
str
orNone
, defaultNone
) – Register the value for this view so that others may link to it.labels (
dict
ofstr
orNone
, 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 (
ViewBox
orNone
, defaultNone
) – Have the PlotItem use the providedViewBox
. If not specified, the PlotItem will be constructed with this as its ViewBox.axisItems (
dict
of{'left', 'bottom', 'right', 'top }
andAxisItem
orNone
) – Pass pre-constructedAxisItem
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 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 (
ImageItem
orlist
ofImageItem
) – SeesetImageItem()
for details.**kwargs (
dict
, optional) – Keyword arguments passed to theColorBarItem
constructor.
- Returns:
The newly created
ColorBarItem
instance.- Return type:
- addItem(
- item,
- *args,
- **kwargs,
Add a
GraphicsItem
to 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.**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
LegendItem
has already been created using this method, that item will be returned rather than creating a new one.- Parameters:
offset (
tuple
ofint
,int
) – The distance to offset the LegendItem, defaults to(30, 30)
.**kwargs (
dict
, optional) – Keyword argument passed to theLegendItem
constructor.
- Returns:
The instance of
LegendItem
that was constructed.- Return type:
- addLine(
- x=None,
- y=None,
- z=None,
- **kwargs,
Create a new
InfiniteLine
and add it to the plot.- Parameters:
x (
float
orNone
) – Position in the x-axis to draw the line. If specified, the line will be vertical. DefaultNone
.y (
float
orNone
) – Position in the y-axis to draw the line. If specified, the line will be horizontal. DefaultNone
.z (
int
orNone
) – 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 theInfiniteLine
instance.
- Returns:
The new
InfiniteLine
added.- Return type:
- clipToViewMode() bool [source]#
Return whether clip-to-view mode is enabled.
- Returns:
True
if clip-to-view mode is enabled,False
otherwise.- Return type:
- 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:
Return whether the context menu is enabled.
- Returns:
True
if the context menu is enabled,False
otherwise.- Return type:
- 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
andy_n
arendarray
data for each curve. Sincex
andy
values 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
PlotDataItem
instances representing the new curves.- Return type:
list
ofPlotDataItem
- plot(
- *args,
- **kwargs,
Add and return a new
PlotDataItem
.- Parameters:
*args (
tuple
, optional) – Arguments that are passed to thePlotDataItem
constructor.**kwargs (
dict
, optional) – Keyword arguments that are passed to thePlotDataItem
constructor. 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
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 thePlotDataItem
constructor.**kwargs (
dict
, optional) – Keyword arguments that are passed to thePlotDataItem
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 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:
- 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.
- 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
orbool
orNone
, 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 (
bool
orNone
, 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.
- 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 whichAxisItem
to 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 keywordtitle
can 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:
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
orbool
, optional) – Whether to enable or disable the ViewBox context menu. If ‘same’, the ViewBox menu will be enabled or disabled as enableMenu. IfTrue
orFalse
, the ViewBox menu will be set accordingly. IfNone
, the ViewBox menu will not be changed. By default ‘same’.
- showAxes(
- selection,
- showValues=True,
- size=False,
Convenience method for quickly configuring axis settings.
- Parameters:
selection (
bool
ortuple
ofbool
) – 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 (
bool
ortuple
ofbool
, 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
ortuple
offloat
, 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. IfNone
is given instead of a float value, the axis reverts to automatic allocation of space. If in tuple form, order is(width, height)
.
- showAxis( )[source]#
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.
- 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:
- 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.