Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c05537d

Browse files
authored
Merge pull request #13986 from anntzer/pcolorfast-quadmesh-rgba
Support RGBA for quadmesh mode of pcolorfast.
2 parents 44952ed + 4903d36 commit c05537d

File tree

3 files changed

+27
-34
lines changed

3 files changed

+27
-34
lines changed
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
Added support for RGB(A) images in pcolorfast
22
`````````````````````````````````````````````
33

4-
pcolorfast now accepts 3D images (RGB or RGBA) arrays if the X and Y
5-
specifications allow image or pcolorimage rendering; they remain unsupported by
6-
the more general quadmesh rendering
4+
pcolorfast now accepts 3D images (RGB or RGBA) arrays.

lib/matplotlib/axes/_axes.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6191,12 +6191,18 @@ def pcolorfast(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
61916191
Parameters
61926192
----------
61936193
C : array-like(M, N)
6194-
A 2D array or masked array. The values will be color-mapped.
6195-
This argument can only be passed positionally.
6194+
The image data. Supported array shapes are:
61966195
6197-
C can in some cases be 3D with the last dimension as rgb(a).
6198-
This is available when C qualifies for image or pcolorimage type,
6199-
will throw a TypeError if C is 3D and quadmesh.
6196+
- (M, N): an image with scalar data. The data is visualized
6197+
using a colormap.
6198+
- (M, N, 3): an image with RGB values (0-1 float or 0-255 int).
6199+
- (M, N, 4): an image with RGBA values (0-1 float or 0-255 int),
6200+
i.e. including transparency.
6201+
6202+
The first two dimensions (M, N) define the rows and columns of
6203+
the image.
6204+
6205+
This parameter can only be passed positionally.
62006206
62016207
X, Y : tuple or array-like, default: ``(0, N)``, ``(0, M)``
62026208
*X* and *Y* are used to specify the coordinates of the
@@ -6221,9 +6227,9 @@ def pcolorfast(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
62216227
- Use 2D arrays *X*, *Y* if you need an *arbitrary quadrilateral
62226228
grid* (i.e. if the quadrilaterals are not rectangular).
62236229
6224-
In this case *X* and *Y* are 2D arrays with shape (M, N),
6230+
In this case *X* and *Y* are 2D arrays with shape (M + 1, N + 1),
62256231
specifying the x and y coordinates of the corners of the colored
6226-
quadrilaterals. See `~.Axes.pcolormesh` for details.
6232+
quadrilaterals.
62276233
62286234
This is the most general, but the slowest to render. It may
62296235
produce faster and more compact output using ps, pdf, and
@@ -6292,10 +6298,6 @@ def pcolorfast(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
62926298
else:
62936299
style = "pcolorimage"
62946300
elif x.ndim == 2 and y.ndim == 2:
6295-
if C.ndim > 2:
6296-
raise ValueError(
6297-
'pcolorfast needs to use quadmesh, '
6298-
'which is not supported when x and y are 2D and C 3D')
62996301
style = "quadmesh"
63006302
else:
63016303
raise TypeError("arguments do not match valid signatures")
@@ -6305,9 +6307,15 @@ def pcolorfast(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
63056307
if style == "quadmesh":
63066308
# data point in each cell is value at lower left corner
63076309
coords = np.stack([x, y], axis=-1)
6310+
if np.ndim(C) == 2:
6311+
qm_kwargs = {"array": np.ma.ravel(C)}
6312+
elif np.ndim(C) == 3:
6313+
qm_kwargs = {"color": np.ma.reshape(C, (-1, C.shape[-1]))}
6314+
else:
6315+
raise ValueError("C must be 2D or 3D")
63086316
collection = mcoll.QuadMesh(
6309-
nc, nr, coords,
6310-
array=np.ma.ravel(C), alpha=alpha, cmap=cmap, norm=norm,
6317+
nc, nr, coords, **qm_kwargs,
6318+
alpha=alpha, cmap=cmap, norm=norm,
63116319
antialiased=False, edgecolors="none")
63126320
self.add_collection(collection, autolim=False)
63136321
xl, xr, yb, yt = x.min(), x.max(), y.min(), y.max()
@@ -6331,7 +6339,7 @@ def pcolorfast(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
63316339

63326340
if vmin is not None or vmax is not None:
63336341
ret.set_clim(vmin, vmax)
6334-
else:
6342+
elif np.ndim(C) == 2: # C.ndim == 3 is RGB(A) so doesn't need scaling.
63356343
ret.autoscale_None()
63366344

63376345
ret.sticky_edges.x[:] = [xl, xr]

lib/matplotlib/tests/test_axes.py

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5151,27 +5151,14 @@ def test_no_None():
51515151
mpl.collections.QuadMesh),
51525152
]
51535153
)
5154-
def test_pcolorfast_colormapped(xy, cls):
5154+
@pytest.mark.parametrize(
5155+
"data", [np.arange(12).reshape((3, 4)), np.random.rand(3, 4, 3)]
5156+
)
5157+
def test_pcolorfast(xy, data, cls):
51555158
fig, ax = plt.subplots()
5156-
data = np.arange(12).reshape((3, 4))
51575159
assert type(ax.pcolorfast(*xy, data)) == cls
51585160

51595161

5160-
def test_pcolor_fast_RGB():
5161-
5162-
fig, ax = plt.subplots(1, 1)
5163-
5164-
np.random.seed(19680801)
5165-
C = np.random.rand(10, 10, 3) # RGB image [0,1]
5166-
x = np.arange(11, dtype=np.float)
5167-
y = np.arange(11, dtype=np.float)
5168-
5169-
xv, yv = np.meshgrid(x, y)
5170-
5171-
with pytest.raises(ValueError):
5172-
ax.pcolorfast(xv, yv, C)
5173-
5174-
51755162
def test_shared_scale():
51765163
fig, axs = plt.subplots(2, 2, sharex=True, sharey=True)
51775164

0 commit comments

Comments
 (0)