matplotlib.pyplot.pcolormesh#

matplotlib.pyplot.pcolormesh(*args,alpha=None,norm=None,cmap=None,vmin=None,vmax=None,colorizer=None,shading=None,antialiased=False,data=None,**kwargs)[source]#

Create a pseudocolor plot with a non-regular rectangular grid.

Call signature:

pcolormesh([X,Y,]C,/,**kwargs)

X andY can be used to specify the corners of the quadrilaterals.

The argumentsX,Y,C are positional-only.

Hint

pcolormesh is similar topcolor. It is much fasterand preferred in most cases. For a detailed discussion on thedifferences seeDifferences between pcolor() and pcolormesh().

Parameters:
Carray-like

The mesh data. Supported array shapes are:

  • (M, N) or M*N: a mesh with scalar data. The values are mapped tocolors using normalization and a colormap. See parametersnorm,cmap,vmin,vmax.

  • (M, N, 3): an image with RGB values (0-1 float or 0-255 int).

  • (M, N, 4): an image with RGBA values (0-1 float or 0-255 int),i.e. including transparency.

The first two dimensions (M, N) define the rows and columns ofthe mesh data.

X, Yarray-like, optional

The coordinates of the corners of quadrilaterals of a pcolormesh:

(X[i+1, j], Y[i+1, j])       (X[i+1, j+1], Y[i+1, j+1])                      ●╶───╴●                      │     │                      ●╶───╴●    (X[i, j], Y[i, j])       (X[i, j+1], Y[i, j+1])

Note that the column index corresponds to the x-coordinate, andthe row index corresponds to y. For details, see theNotes section below.

Ifshading='flat' the dimensions ofX andY should be onegreater than those ofC, and the quadrilateral is colored dueto the value atC[i,j]. IfX,Y andC have equaldimensions, a warning will be raised and the last row and columnofC will be ignored.

Ifshading='nearest' or'gouraud', the dimensions ofXandY should be the same as those ofC (if not, a ValueErrorwill be raised). For'nearest' the colorC[i,j] iscentered on(X[i,j],Y[i,j]). For'gouraud', a smoothinterpolation is carried out between the quadrilateral corners.

IfX and/orY are 1-D arrays or column vectors they will beexpanded as needed into the appropriate 2D arrays, making arectangular grid.

cmapstr orColormap, default:rcParams["image.cmap"] (default:'viridis')

The Colormap instance or registered colormap name used to map scalar datato colors.

normstr orNormalize, optional

The normalization method used to scale scalar data to the [0, 1] rangebefore mapping to colors usingcmap. By default, a linear scaling isused, mapping the lowest value to 0 and the highest to 1.

If given, this can be one of the following:

vmin, vmaxfloat, optional

When using scalar data and no explicitnorm,vmin andvmax definethe data range that the colormap covers. By default, the colormap coversthe complete value range of the supplied data. It is an error to usevmin/vmax when anorm instance is given (but using astrnormname together withvmin/vmax is acceptable).

colorizerColorizer or None, default: None

The Colorizer object used to map color to data. If None, a Colorizerobject is created from anorm andcmap.

edgecolors{'none', None, 'face', color, color sequence}, optional

The color of the edges. Defaults to 'none'. Possible values:

The singular formedgecolor works as an alias.

alphafloat, default: None

The alpha blending value, between 0 (transparent) and 1 (opaque).

shading{'flat', 'nearest', 'gouraud', 'auto'}, optional

The fill style for the quadrilateral; defaults torcParams["pcolor.shading"] (default:'auto'). Possible values:

  • 'flat': A solid color is used for each quad. The color of thequad (i, j), (i+1, j), (i, j+1), (i+1, j+1) is given byC[i,j]. The dimensions ofX andY should beone greater than those ofC; if they are the same asC,then a deprecation warning is raised, and the last rowand column ofC are dropped.

  • 'nearest': Each grid point will have a color centered on it,extending halfway between the adjacent grid centers. Thedimensions ofX andY must be the same asC.

  • 'gouraud': Each quad will be Gouraud shaded: The color of thecorners (i', j') are given byC[i',j']. The color values ofthe area in between is interpolated from the corner values.The dimensions ofX andY must be the same asC. WhenGouraud shading is used,edgecolors is ignored.

  • 'auto': Choose 'flat' if dimensions ofX andY are onelarger thanC. Choose 'nearest' if dimensions are the same.

Seepcolormesh grids and shadingfor more description.

snapbool, default: False

Whether to snap the mesh to pixel boundaries.

rasterizedbool, optional

Rasterize the pcolormesh when drawing vector graphics. This canspeed up rendering and produce smaller files for large data sets.See alsoRasterization for vector graphics.

Returns:
matplotlib.collections.QuadMesh
Other Parameters:
dataindexable object, optional

If given, all parameters also accept a strings, which isinterpreted asdata[s] ifs is a key indata.

**kwargs

Additionally, the following arguments are allowed. They are passedalong to theQuadMesh constructor:

Property

Description

agg_filter

a filter function, which takes a (m, n, 3) float array and a dpi value, and returns a (m, n, 3) array and two offsets from the bottom left corner of the image

alpha

array-like or float or None

animated

bool

antialiased oraa orantialiaseds

bool or list of bools

array

array-like

capstyle

CapStyle or {'butt', 'projecting', 'round'}

clim

(vmin: float, vmax: float)

clip_box

BboxBase or None

clip_on

bool

clip_path

Patch or (Path, Transform) or None

cmap

Colormap or str or None

color

color or list of RGBA tuples

edgecolor orec oredgecolors

color or list ofcolor or 'face'

facecolor orfacecolors orfc

color or list ofcolor

figure

Figure orSubFigure

gid

str

hatch

{'/', '\', '|', '-', '+', 'x', 'o', 'O', '.', '*'}

hatch_linewidth

unknown

in_layout

bool

joinstyle

JoinStyle or {'miter', 'round', 'bevel'}

label

object

linestyle ordashes orlinestyles orls

str or tuple or list thereof

linewidth orlinewidths orlw

float or list of floats

mouseover

bool

norm

Normalize or str or None

offset_transform ortransOffset

Transform

offsets

(N, 2) or (2,) array-like

path_effects

list ofAbstractPathEffect

picker

None or bool or float or callable

pickradius

float

rasterized

bool

sketch_params

(scale: float, length: float, randomness: float)

snap

bool or None

transform

Transform

url

str

urls

list of str or None

visible

bool

zorder

float

See also

pcolor

An alternative implementation with slightly different features. For a detailed discussion on the differences seeDifferences between pcolor() and pcolormesh().

imshow

IfX andY are each equidistant,imshow can be a faster alternative.

Notes

Masked arrays

C may be a masked array. IfC[i,j] is masked, the correspondingquadrilateral will be transparent. Masking ofX andY is notsupported. Usepcolor if you need this functionality.

Grid orientation

The grid orientation follows the standard matrix convention: An arrayC with shape (nrows, ncolumns) is plotted with the column number asX and the row number asY.

Differences between pcolor() and pcolormesh()

Both methods are used to create a pseudocolor plot of a 2D arrayusing quadrilaterals.

The main difference lies in the created object and internal datahandling:Whilepcolor returns aPolyQuadMesh,pcolormeshreturns aQuadMesh. The latter is more specialized for the givenpurpose and thus is faster. It should almost always be preferred.

There is also a slight difference in the handling of masked arrays.Bothpcolor andpcolormesh support masked arraysforC. However, onlypcolor supports masked arrays forXandY. The reason lies in the internal handling of the masked values.pcolor leaves out the respective polygons from thePolyQuadMesh.pcolormesh sets the facecolor of the maskedelements to transparent. You can see the difference when usingedgecolors. While all edges are drawn irrespective of masking in aQuadMesh, the edge between two adjacent masked quadrilaterals inpcolor is not drawn as the corresponding polygons do notexist in the PolyQuadMesh. Because PolyQuadMesh draws each individualpolygon, it also supports applying hatches and linestyles to the collection.

Another difference is the support of Gouraud shading inpcolormesh, which is not available withpcolor.

Examples usingmatplotlib.pyplot.pcolormesh#

pcolor images

pcolor images

pcolormesh grids and shading

pcolormesh grids and shading

pcolormesh

pcolormesh

QuadMesh Demo

QuadMesh Demo

Rasterization for vector graphics

Rasterization for vector graphics

Time Series Histogram

Time Series Histogram