how to pcolor all value of a matrix ?

50 views (last 30 days)
clear all
clc
a=[100 0 50; 30 5 100; 80 50 0]
b=[a ;a(end,:)] ;
b=[b b(:,end)]
subplot(1,2,1)
pcolor(a)
subplot(1,2,2)
pcolor(b)
%%
Hey guys, I tried to plot all the value of the matrix "a" by using pcolor, I expected to obtain 9 values on the graph since " a" is 3*3 Martix but Matlab skip the last row and last Column and I only obtained 2*2 value, so I tried to create a new matrix called " b" by reading the last column and row.
is their a way to pcolor all the value of "a" without skipping any data and not creating every time a new matrix?
Thank you in advance

Accepted Answer

Walter Roberson
Walter Roberson on 6 Jan 2022
No, there is no way to do that.
pcolor(a) is surf(a); view(2) and surf() creates face colors by interpolation of the four corners of the face. surf() and so pcolor() will always "lose" a row and column and there is no way to get around that.
You should be using imagesc() if you have a flat plane with equidistant faces. You should be using warp() if you need to wrap an image to a curved surface.
  4 Comments
Voss
Voss on 1 Dec 2022
Edited: Voss on 1 Dec 2022
"What if your data is not equidistant?"
pcolor supports non-equidistant data (so does surf).
% x, y vectors defining the corners:
x = [1 10 25 50];
y = [2 4 8 16];
% z is one smaller:
z = magic(3);
% augment z with NaNs:
z_plot = z;
z_plot(:,end+1) = NaN;
z_plot(end+1,:) = NaN;
% make the surface:
pcolor(x,y,z_plot)
"How can matlab afterall these years not ..."
It's easy enough to make your own function to do it:
function h = my_pcolor(x,y,z)
% augment z with NaNs:
z_plot = z;
z_plot(:,end+1) = NaN;
z_plot(end+1,:) = NaN;
% make the surface:
h = pcolor(x,y,z_plot)
end
You can also easily make your function accept any inputs arguments that pcolor accepts:
function h = my_pcolor(x,y,z,varargin)
% augment z with NaNs:
z_plot = z;
z_plot(:,end+1) = NaN;
z_plot(end+1,:) = NaN;
% make the surface:
h = pcolor(x,y,z_plot,varargin{:})
end
g b
g b on 19 Feb 2023
As mentioned, for uniformely spaced data imagesc is the better option which leaves the case of non-uniformely spaced coordinates. Thank you Mr Roberson for bringing the attention to warp. It seems to be a reasonable option (although it requires the image processing toolbox and is a bit awkward with colormap, alpha etc.).
If the given coordinates (x and y) represent "centers" of cells and the values (Z) represent some form of averages across those cells and if someone wants to plot this data in the form of uniformly colored patches, then the problem would be to find the patch-vertices (cell-edges) from the given cell-centers. warp does not fully map the patches to this concept although I do think that it is a viable option.
Another way could be inter-extrapolation to cell-edges (as in the example) which does not locate the given coordinates into the cell-centers either but expands the presentation a bit along the outline of the domain.
To properly determine cell-edges so that the given coordinates are always centered within the cells can result in gaps and/or overlaps between cells which would prevent the use of surf etc. I suppose, if someone wants to plot 2D data in the form of correctly spaced, correctly sized and uniformly colored patches, it is a good idea to know something about the cell-edges.
x = power(ones(1,5)*2,1:5);
y = (0:4).^2;
[X,Y] = meshgrid(x,y);
Z = sqrt(X.^2 + Y.^2);
Z(2,3) = NaN;
%% linear inter- and extra-polation to grid edges
xn = interp1(1:numel(x),x,0.5:numel(x)+.5,'linear','extrap');
yn = interp1(1:numel(y),y,0.5:numel(y)+.5,'linear','extrap');
%%
[Xn,Yn] = meshgrid(xn,yn);
Zn = Z;
Zn(:,end+1) = NaN;
Zn(end+1,:) = NaN;
cl = [min(Z(:)) max(Z(:))];
ax(1) = subplot(2,2,1);
title('imagesc')
hold on
imagesc(x,y,Z,'alphadata',~isnan(Z))
plot(X,Y,'.k')
caxis(cl)
ax(2) = subplot(2,2,2);
title('pcolor')
hold on
pcolor(x,y,Z)
plot(X,Y,'.k')
caxis(cl)
ax(3) = subplot(2,2,3);
title('warp')
hold on
hw = warp(X,Y,zeros(size(Z)),Z); view(2)
hw.FaceAlpha = 'texturemap';
alpha(hw,double(~isnan(Z)))
colormap(parula)
plot(X,Y,'.k')
caxis(cl)
axis xy
ax(4) = subplot(2,2,4);
title('pcolor + linear edge grid')
hold on
pcolor(xn,yn,Zn)
plot(X,Y,'.k')
caxis(cl)
linkaxes(ax,'xy')
ax(1).XLim = [xn(1)-4 xn(end)+1];
ax(1).YLim = [yn(1)-2 yn(end)+1];

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!