how to do pcolor plots from 3d data?

34 views (last 30 days)
Hi all,
I have a data set of the type
x=100;
y=100;
z=100;
r = randi([1 5],x,y,z);
I would like to plot a contourplot from pcolor or contourf (2D data from r in z=60) which colors only all number 1's (like r(:,:,60)=1) with red color, in z=60. The other numbers are represented by the transparent pixels.
I tested
f = figure(1);
pcolor(r(:,:,60)==1);
shading interp;
hcb=colorbar;
But does not work. Can anybody help me?
Thank you so much in advance.

Accepted Answer

Walter Roberson
Walter Roberson on 16 Jan 2022
x=100;
y=100;
z=100;
r = randi([1 5],x,y,z);
cmap = [0 0 0; 1 0 0];
mask = double(r(:,:,60) == 1);
imagesc(mask, 'AlphaData', mask);
colormap(cmap);
set(gca, 'XDir', 'normal', 'YDir', 'normal');
figure
h = pcolor(mask);
h.AlphaData = mask;
colormap(cmap)
set(gca, 'XDir', 'normal', 'YDir', 'normal');
If you look carefully, you will notice a lot of differences. Remember that pcolor() interpolates faces, pcolor() does not produce images -- pcolor() produces surfaces ... viewed from above. pcolor() is literally surf() followed by view(2) and surf() treats the input data as vertex colors and interpolates the color of faces from the four corners.
I would suggest to you that it would make more sense for you to use image() or imagesc() than to use pcolor()
  8 Comments
Voss
Voss on 16 Jan 2022
If you want to color the elements that are closer to 1 than they are to any other integer, you can just round the data:
dc = load('savedata2.mat');
dc1 = struct2cell(dc);
r = cat(3,dc1{:});
cmap = [0 0 0; 1 0 0];
mask = double(round(r(:,:,25)) == 1);
imagesc(mask, 'AlphaData', mask);
colormap(cmap);
If, however, you want to color the elements whose difference from 1 is less than or equal to some threshold, e.g., within 0.5 or 0.001 of 1, you can do that too:
threshold = 0.5;
mask = double(abs(r(:,:,25)-1) <= threshold);
imagesc(mask, 'AlphaData', mask);
colormap(cmap);
threshold = 0.1;
mask = double(abs(r(:,:,25)-1) <= threshold);
imagesc(mask, 'AlphaData', mask);
colormap(cmap);
threshold = 0.01;
mask = double(abs(r(:,:,25)-1) <= threshold);
imagesc(mask, 'AlphaData', mask);
colormap(cmap);
threshold = 0.001;
mask = double(abs(r(:,:,25)-1) <= threshold);
imagesc(mask, 'AlphaData', mask);
colormap(cmap);
threshold = 0.0001;
mask = double(abs(r(:,:,25)-1) <= threshold);
imagesc(mask, 'AlphaData', mask);
colormap(cmap);
SAC CSA
SAC CSA on 16 Jan 2022
@Benjamin, is perfect. Thank you so much for your kindness.

Sign in to comment.

More Answers (1)

KSSV
KSSV on 16 Jan 2022
This is a 3D data, read about slice.

Categories

Find more on Author Block Masks in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!