You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello,
When using imshow, it's possible to pass either a 2D array (which is rendered as false color via a colormap) or a 3D array where the last dimension is 3 (or perhaps 4, I've not tested). In the latter case, the data is rendered assuming the last dimension means color, i.e. the data is in RGB format. When using pcolormesh, however, only the first behaviour works; passing RGB data simply results in an error:
Traceback (most recent call last):
File "C:\Users\Richard\Desktop\2015_02_10_CB7_Nap\test_rgb_idealised.py", line 19, in <module>
m = plt.pcolormesh(x, y, rgb/255.0, linewidth=0)
File "C:\Anaconda\lib\site-packages\matplotlib\pyplot.py", line 2946, in pcolormesh
ret = ax.pcolormesh(*args, **kwargs)
File "C:\Anaconda\lib\site-packages\matplotlib\axes.py", line 7736, in pcolormesh
X, Y, C = self._pcolorargs('pcolormesh', *args, allmatch=allmatch)
File "C:\Anaconda\lib\site-packages\matplotlib\axes.py", line 7351, in _pcolorargs
numRows, numCols = C.shape
ValueError: too many values to unpack
This might be completely identical to the above issue, but after having this same problem, I tried passing RGB arrays to the color= parameter of pcolor and pcolormesh, and it gives the error:
TypeError: Illegal arguments to pcolormesh; see help(pcolormesh)
Even though it is accepted for the above-linked workaround (i.e. only passing a single array to pcolor or pcolormesh).
Hello,
I suspect it might be identical - there were two things that confused me initially; firstly the color parameter did nothing unless I also specified a monochrome array (I guess that's what sets size, etc.) and secondly the format of color is not what I expected: the workaround I link to above shows it, but it took me a while to realise that it wanted a 2D array (one dimension being point number and the other being colour channel) rather than a 3D array (with X, Y and colour as the dimensions).
Perhaps one of those is the problem you're seeing? Apologies if all those points are entirely obvious...
In the proposed workaround you do not parse coordinates to pcolormesh and the resulting RGB is just fine. But if one explicitely specifies coordinate arrays, the resulting RGB is somehow shuffled. Example:
import matplotlib.pyplot as plt
import numpy as np
# Define coordinate arrays
N = 100
x = np.arange(N)
y = np.arange(N)
x, y = np.meshgrid(x, y)
# Define RGB arrays
r = np.zeros(x.shape)
g = np.zeros(x.shape)
b = np.zeros(x.shape)
# ... brush along the diagonal
r[range(N), range(N)] = np.linspace(0, 1, N)
g[range(N), range(N)] = np.linspace(0, 1, N)
b[range(N), range(N)] = np.linspace(0, 1, N)
# ... brush vertically
r[:, N/2.0] = np.linspace(0, 1, N)
g[:, N/2.0] = np.linspace(0, 1, N)
b[:, N/2.0] = np.linspace(0, 1, N)
# ... Stack them
rgb = np.dstack([r, g, b])
# Reshape as list of rgb tuples
color_tuples = np.array([r.flatten(), g.flatten(), b.flatten()]).transpose()
# Plot using imshow
plt.figure()
plt.imshow(rgb, origin='lower', interpolation='none')
plt.title('imshow')
# Plot using pcolormesh (with coordinates)
plt.figure()
im = plt.pcolormesh(x, y, r, color=color_tuples)
im.set_array(None)
plt.title('pcolormesh with coordinates')
# Plot using pcolormesh (without coordinates)
plt.figure()
im = plt.pcolormesh(r, color=color_tuples)
im.set_array(None)
plt.title('pcolormesh without coordinates')
plt.show()
Remember, pcolormesh is based on specifying the corners, not the centers, of the quadrilaterals. Therefore your x and y arrays should have length N+1, not N. Pcolormesh accepts the smaller coordinate arrays by chopping the last row and column off of the Z array. This behavior was included in the earliest days for the sake of Matlab compatibility. I don't know whether it is still true, but at that time Matlab always ignored the last row and column of the Z array.
Ah, I see. If the last row & column of the data array are being chopped off, the RGB tuples do not match the data anymore. If the coordinate arrays have length N+1 the result is as expected. Thanks for pointing that out!
It would be really nice if there was a simple explanation about how to realize the old behaviour of imshow with RGB data, for example, when trying to load a saved PNG image that was already using log scaling for the axes. I mentioned this in issue 7661 but since that is closed, it seems to have been ignored.
I can make it work, but it is extremely slow. The data first seems to be scaled logarithmically since the abscissa are not evenly space, then scaled back when I set the axes to have logarithmic scaling.
It took me a while to understand what @efiring was saying but I figured it out. Very helpful. I edited the sample given by @sfinkens (very helpful as well!) as:
import matplotlib.pyplot as plt
import numpy as np
# Define coordinate arrays
N = 10
x = np.arange(N+1)
y = np.arange(N+1)
x, y = np.meshgrid(x, y)
# Define RGB arrays
r = np.zeros([x.shape[0]-1,x.shape[1]-1])
g = np.zeros([x.shape[0]-1,x.shape[1]-1])
b = np.zeros([x.shape[0]-1,x.shape[1]-1])
# ... brush along the diagonal
r[range(N), range(N)] = np.linspace(0, 1, N)
g[range(N), range(N)] = np.linspace(0, 1, N)
b[range(N), range(N)] = np.linspace(0, 1, N)
# ... brush vertically
r[:, int(N/2.0)] = np.linspace(0, 1, N)
g[:, int(N/2.0)] = np.linspace(0, 1, N)
b[:, int(N/2.0)] = np.linspace(0, 1, N)
# ... Stack them
rgb = np.dstack([r, g, b])
# Reshape as list of rgb tuples
color_tuples = np.array([r.flatten(), g.flatten(), b.flatten()]).transpose()
# Plot using imshow
plt.figure()
plt.imshow(rgb, origin='lower', interpolation='none')
plt.title('imshow')
# Plot using pcolormesh (with coordinates)
plt.figure()
im = plt.pcolormesh(x, y, r, color=color_tuples)
im.set_array(None)
plt.title('pcolormesh with coordinates')
# Plot using pcolormesh (without coordinates)
plt.figure()
im = plt.pcolormesh(r, color=color_tuples)
im.set_array(None)
plt.title('pcolormesh without coordinates')
plt.show()
Activity
lzkelley commentedon Dec 9, 2015
This might be completely identical to the above issue, but after having this same problem, I tried passing RGB arrays to the
color=parameter ofpcolorandpcolormesh, and it gives the error:Even though it is accepted for the above-linked workaround (i.e. only passing a single array to
pcolororpcolormesh).rwb27 commentedon Jan 5, 2016
Hello,
I suspect it might be identical - there were two things that confused me initially; firstly the
colorparameter did nothing unless I also specified a monochrome array (I guess that's what sets size, etc.) and secondly the format ofcoloris not what I expected: the workaround I link to above shows it, but it took me a while to realise that it wanted a 2D array (one dimension being point number and the other being colour channel) rather than a 3D array (with X, Y and colour as the dimensions).Perhaps one of those is the problem you're seeing? Apologies if all those points are entirely obvious...
Richard
sfinkens commentedon Oct 14, 2016
In the proposed workaround you do not parse coordinates to pcolormesh and the resulting RGB is just fine. But if one explicitely specifies coordinate arrays, the resulting RGB is somehow shuffled. Example:
lzkelley commentedon Oct 14, 2016
For convenience, the result of the @Funkensieper (very helpful) example above is:
efiring commentedon Oct 14, 2016
Remember, pcolormesh is based on specifying the corners, not the centers, of the quadrilaterals. Therefore your x and y arrays should have length N+1, not N. Pcolormesh accepts the smaller coordinate arrays by chopping the last row and column off of the Z array. This behavior was included in the earliest days for the sake of Matlab compatibility. I don't know whether it is still true, but at that time Matlab always ignored the last row and column of the Z array.
sfinkens commentedon Oct 16, 2016
Ah, I see. If the last row & column of the data array are being chopped off, the RGB tuples do not match the data anymore. If the coordinate arrays have length N+1 the result is as expected. Thanks for pointing that out!
mforbes commentedon Sep 23, 2018
It would be really nice if there was a simple explanation about how to realize the old behaviour of
imshowwith RGB data, for example, when trying to load a saved PNG image that was already using log scaling for the axes. I mentioned this in issue 7661 but since that is closed, it seems to have been ignored.jklymak commentedon Sep 23, 2018
Doesn’t the example above do it for you?
mforbes commentedon Sep 23, 2018
I can make it work, but it is extremely slow. The data first seems to be scaled logarithmically since the abscissa are not evenly space, then scaled back when I set the axes to have logarithmic scaling.
javierconcha commentedon Mar 4, 2020
It took me a while to understand what @efiring was saying but I figured it out. Very helpful. I edited the sample given by @sfinkens (very helpful as well!) as:
Giving me the right plot:

efiring commentedon Mar 4, 2020
With #13986 pcolorfast is implementing the desired behavior here, directly supporting a third argument with 3 dimensions, the last being RGB or RGBA.
anntzer commentedon Mar 4, 2020
Given that pcolorfast supports RGBA inputs in quadmesh form, it "should" really not be that hard to make pcolormesh also support such inputs :)
efiring commentedon Mar 4, 2020
Agreed. It's a little more complicated than for pcolorfast because of the range of options pcolormesh accepts.
anntzer commentedon Mar 4, 2020
that's why I put in quotes :-)
(I had a quick look, I think it's basically the interaction with shading that needs to be handled)