Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 66d1605

Browse filesBrowse files
committed
Support RGBA for quadmesh mode of pcolorfast.
1 parent a3e2897 commit 66d1605
Copy full SHA for 66d1605

File tree

Expand file treeCollapse file tree

3 files changed

+25
-32
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+25
-32
lines changed
+1-3Lines 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

Copy file name to clipboardExpand all lines: lib/matplotlib/axes/_axes.py
+20-12Lines changed: 20 additions & 12 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
@@ -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": C}
6312+
elif np.ndim(C) == 3:
6313+
qm_kwargs = {"color": C.reshape((-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:
63356343
ret.autoscale_None()
63366344

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

‎lib/matplotlib/tests/test_axes.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_axes.py
+4-17Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5197,27 +5197,14 @@ def test_no_None():
51975197
mpl.collections.QuadMesh),
51985198
]
51995199
)
5200-
def test_pcolorfast_colormapped(xy, cls):
5200+
@pytest.mark.parametrize(
5201+
"data", [np.arange(12).reshape((3, 4)), np.random.rand(3, 4, 3)]
5202+
)
5203+
def test_pcolorfast(xy, data, cls):
52015204
fig, ax = plt.subplots()
5202-
data = np.arange(12).reshape((3, 4))
52035205
assert type(ax.pcolorfast(*xy, data)) == cls
52045206

52055207

5206-
def test_pcolor_fast_RGB():
5207-
5208-
fig, ax = plt.subplots(1, 1)
5209-
5210-
np.random.seed(19680801)
5211-
C = np.random.rand(10, 10, 3) # RGB image [0,1]
5212-
x = np.arange(11, dtype=np.float)
5213-
y = np.arange(11, dtype=np.float)
5214-
5215-
xv, yv = np.meshgrid(x, y)
5216-
5217-
with pytest.raises(ValueError):
5218-
ax.pcolorfast(xv, yv, C)
5219-
5220-
52215208
def test_shared_scale():
52225209
fig, axs = plt.subplots(2, 2, sharex=True, sharey=True)
52235210

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.