Why can't pcolor show the entire matrix to plot?

I am trying to plot a pcolor of the matrix Error3D which is size 300x300.
Error3D is mostly NaNs except for Error3D(:,100), Error3D(:,200) and Error3D(:,300).
When I try to plot the matrix is shows the correct values for Error3D(:,100) and Error3D(:,200) but the values for Error3D(:,300) do not appear.
(please see attached figure)
Here is my code:
load('Error3D.mat')
figure(1)
clf;
hold on;
pcolor(Error3D);
cb = colorbar;
Does anyone know how to fix this issue and make the values of Error3D(:,300) visible on my plot?
Thank you

 Réponse acceptée

DGM
DGM le 18 Août 2021
Modifié(e) : DGM le 18 Août 2021
The plot appears black because a pcolor() plot is basically like a surf() plot. The faces are all outlined by black edges. When the mesh is dense, all you see is edges.
After the pcolor() call, do this:
shading flat
or just set the EdgeColor property of the pcolor() plot to 'none'.
h = pcolor(Error3D);
set(h,'edgecolor','none');
Either one will basically do the same thing.

5 commentaires

A LL
A LL le 18 Août 2021
Thanks DGM for your answer.
Indeed, it looks better with the shading flat command you propose but it does not solve my question.
Error3D(:,300) is not visible on my plot while we can see Error3D(:,100), Error3D(:,200) just fine.
Do you happen to know why the last column of the matrix Error3D is not shown?
Thank you
I hadn't stopped to notice that 300 was at the edge of the array in your case. IA is right. With pcolor(), the values are associated with the vertices of the mesh, just like with surf() or mesh(). Using image() or imagesc might be fine.
Part of the issue is likely the fact that the plot box obscures the last pixel. Like you said, padding the array helps make it visible. It's also pretty faint, so a lot of the colormaps don't give it much contrast.
load('Error3D.mat')
imagesc(Error3D);
set(gca,'ydir','normal')
cb = colorbar;
colormap(1-ccmap)
box off
... but it's there.
A LL
A LL le 18 Août 2021
Thank you very much DGM for your help! It indeed works!
I have to say I find it weird that imagesc attributes the same color for a NaN than for 0. I would have like NaNs to remain white and only have the values of interest displayed in color.
That can be done using alphadata.
% ...
h = imagesc(Error3D);
set(h,'alphadata',~isnan(Error3D))
% ...
Since the default color of the underlying axes is white, that's what shows through.
FWIW, I run an inverted display, so that prior example is inverted as I normally see it. This example is flipped so that it's like you'd normally see it.
A LL
A LL le 19 Août 2021
Great! Thanks again DGM.

Connectez-vous pour commenter.

Plus de réponses (1)

pcolor() does not show the values as little square pixels like the imaging functions (imshow(), imagesc(), and image()) do.
m = randi(255, 3, 3); % Make 3x3 matrix
pcolor(m)
shading 'flat';
Basically it's because it's not the center of the little square that has the value, it's the edge of the square that has the value. Then it makes a plane tilted to the other edge with the colors changing along the way. However if you say shading flat, it doesn't change the color, it basically makes the square flat, not tilted, and so the color will be the color of one of the edges (I'm not sure which edge it uses to decide on the color).
So you can see from the above display of a 3x3 matrix, there are 3 edges but only 2 tiles/squares. For this annoying reason, I don't use pcolor() and use imshow() instead. You also might want to consider that if you just simply want to display a matrix. You can still apply a colormap if you want, and one of your own choosing instead of the one pcolor chooses for you, by calling colormap().

3 commentaires

Thank you Image Analyst for your answer!
I tried the command you proposed by it does not give the correct figure.
(please see attached figures)
Here is what I tried:
load('Error3D.mat')
figure(1); clf;imshow(Error3D);shading flat; colorbar; %gives a black square
figure(2); clf;surf(Error3D),colormap(winter);shading flat; colorbar; %gives an empty figure
N.B. I ended up solving my issue by adding a column of NaNs at the end of my Error3D matrix but I would like to make your answer work.
Thank you
Unfortunately your array is virtually all NaN's so there's virtually nothing to display. But if it weren't this is how to display a gray scale image:
% fileName = 'Error3D.mat'
% s = load(fileName)
% grayImage = s.Error3D;
grayImage = imread('cameraman.tif');
[rows, columns, numberOfColorChannels] = size(grayImage)
% Unfortunately the array is virtualyl all NaNs.
imshow(grayImage);
subplot(2, 1, 1);
cmap = hsv(256);
imshow(grayImage, 'Colormap', cmap);
axis('on', 'image');
colorbar;
For a surface plot, see attached demo.
A LL
A LL le 19 Août 2021
Great! Thanks Image Analyst for your help!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Color and Styling dans Centre d'aide et File Exchange

Produits

Question posée :

le 18 Août 2021

Commenté :

le 19 Août 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by