Pyqtgraph’s Helper Functions

Simple Data Display Functions

pyqtgraph.plot(*args, **kargs)[source]
Create and return a PlotWindow (this is just a window with PlotWidget inside), plot data in it.
Accepts a title argument to set the title of the window.
All other arguments are used to plot data. (see PlotItem.plot())
pyqtgraph.image(*args, **kargs)[source]
Create and return an ImageWindow (this is just a window with ImageView widget inside), show image data inside.
Will show 2D or 3D image data.
Accepts a title argument to set the title of the window.
All other arguments are used to show data. (see ImageView.setImage())

Color, Pen, and Brush Functions

Qt uses the classes QColor, QPen, and QBrush to determine how to draw lines and fill shapes. These classes are highly capable but somewhat awkward to use. Pyqtgraph offers the functions mkColor(), mkPen(), and mkBrush() to simplify the process of creating these classes. In most cases, however, it will be unnecessary to call these functions directly–any function or method that accepts pen or brush arguments will make use of these functions for you. For example, the following three lines all have the same effect:

pg.plot(xdata, ydata, pen='r')
pg.plot(xdata, ydata, pen=pg.mkPen('r'))
pg.plot(xdata, ydata, pen=QPen(QColor(255, 0, 0)))
pyqtgraph.mkColor(*args)

Convenience function for constructing QColor from a variety of argument types. Accepted arguments are:

‘c’ one of: r, g, b, c, m, y, k, w
R, G, B, [A] integers 0-255
(R, G, B, [A]) tuple of integers 0-255
float greyscale, 0.0-1.0
int see intColor()
(int, hues) see intColor()
“RGB” hexadecimal strings; may begin with ‘#’
“RGBA”  
“RRGGBB”  
“RRGGBBAA”  
QColor QColor instance; makes a copy.
pyqtgraph.mkPen(*args, **kargs)

Convenience function for constructing QPen.

Examples:

mkPen(color)
mkPen(color, width=2)
mkPen(cosmetic=False, width=4.5, color='r')
mkPen({'color': "FF0", width: 2})
mkPen(None)   # (no pen)

In these examples, color may be replaced with any arguments accepted by mkColor()

pyqtgraph.mkBrush(*args)
Convenience function for constructing Brush.
This function always constructs a solid brush and accepts the same arguments as mkColor()
Calling mkBrush(None) returns an invisible brush.
pyqtgraph.hsvColor(h, s=1.0, v=1.0, a=1.0)

Generate a QColor from HSVa values.

pyqtgraph.intColor(index, hues=9, values=1, maxValue=255, minValue=150, maxHue=360, minHue=0, sat=255, alpha=255, **kargs)

Creates a QColor from a single index. Useful for stepping through a predefined list of colors.

The argument index determines which color from the set will be returned. All other arguments determine what the set of predefined colors will be

Colors are chosen by cycling across hues while varying the value (brightness). By default, this selects from a list of 9 hues.

pyqtgraph.colorTuple(c)

Return a tuple (R,G,B,A) from a QColor

pyqtgraph.colorStr(c)

Generate a hex string code from a QColor

Data Slicing

pyqtgraph.affineSlice(data, shape, origin, vectors, axes, **kargs)

Take a slice of any orientation through an array. This is useful for extracting sections of multi-dimensional arrays such as MRI images for viewing as 1D or 2D data.

The slicing axes are aribtrary; they do not need to be orthogonal to the original data or even to each other. It is possible to use this function to extract arbitrary linear, rectangular, or parallelepiped shapes from within larger datasets.

For a graphical interface to this function, see ROI.getArrayRegion()

Arguments:

data (ndarray): the original dataset
shape: the shape of the slice to take (Note the return value may have more dimensions than len(shape))
origin: the location in the original dataset that will become the origin in the sliced data.
vectors: list of unit vectors which point in the direction of the slice axes
  • each vector must have the same length as axes
  • If the vectors are not unit length, the result will be scaled.
  • If the vectors are not orthogonal, the result will be sheared.

axes: the axes in the original dataset which correspond to the slice vectors

Example: start with a 4D fMRI data set, take a diagonal-planar slice out of the last 3 axes

  • data = array with dims (time, x, y, z) = (100, 40, 40, 40)
  • The plane to pull out is perpendicular to the vector (x,y,z) = (1,1,1)
  • The origin of the slice will be at (x,y,z) = (40, 0, 0)
  • We will slice a 20x20 plane from each timepoint, giving a final shape (100, 20, 20)

The call for this example would look like:

affineSlice(data, shape=(20,20), origin=(40,0,0), vectors=((-1, 1, 0), (-1, 0, 1)), axes=(1,2,3))

Note the following must be true:

len(shape) == len(vectors)
len(origin) == len(axes) == len(vectors[0])

SI Unit Conversion Functions

pyqtgraph.siFormat(x, precision=3, suffix='', space=True, error=None, minVal=1e-25, allowUnicode=True)

Return the number x formatted in engineering notation with SI prefix.

Example:

siFormat(0.0001, suffix='V')  # returns "100 μV"
pyqtgraph.siScale(x, minVal=1e-25, allowUnicode=True)

Return the recommended scale factor and SI prefix string for x.

Example:

siScale(0.0001)   # returns (1e6, 'μ')
# This indicates that the number 0.0001 is best represented as 0.0001 * 1e6 = 100 μUnits
pyqtgraph.siEval(s)

Convert a value written in SI notation to its equivalent prefixless value

Example:

siEval("100 μV")  # returns 0.0001

Table Of Contents

Previous topic

API Reference

Next topic

Pyqtgraph’s Graphics Items

This Page