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.

 채택된 답변

Walter Roberson
Walter Roberson 2022년 1월 16일

0 개 추천

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

Hi @Walter Roberson, thank you very much for the answer. I tried apply the code to the data attached (which have the same structure of r asked above, except by the dimension 50x50x50), but it does not print any number 1 with respect to z=25, from the data attached.
dc = load('savedata2.mat');
dc1 = struct2cell(dc);
r = cat(3,dc1{:});
cmap = [0 0 0; 1 0 0];
mask = double(r(:,:,25) == 1);
imagesc(mask, 'AlphaData', mask);
colormap(cmap);
set(gca, 'XDir', 'normal', 'YDir', 'normal');
The output shown is
However, there are elements 1's in r(:,:,25), that should be printed. I thought the problem might be in the 'mask' variable. You can help me edit the command to obtains pixel red to relative to 1's?
Voss
Voss 2022년 1월 16일
편집: Walter Roberson 2022년 1월 16일
There are no 1's in r
dc = load('savedata2.mat');
dc1 = struct2cell(dc);
r = cat(3,dc1{:});
find(r == 1)
ans = 0×1 empty double column vector
ur = unique(r);
[~, idx] = min(abs(ur - 1));
ur(idx)
ans = 1.0000
ur(idx) - 1
ans = 4.4176e-07
SAC CSA
SAC CSA 2022년 1월 16일
Hi @Benjamin thank you for the comment. When showing r(:,:,25) it is possible to see 1's yes. Strangely these 1's are not shown when using find or mask = double(r(:,:,25) == 1); See 1's in the screenshot taken when MatLab to show r(:,:,25).
Voss
Voss 2022년 1월 16일
편집: Voss 2022년 1월 16일
Those values are close to 1 but not exactly 1. For instance, the element of r that is closest to 1 is actually approximately 1.00000044176, as my comment shows (thanks to @Walter Roberson for adding that point).
Voss
Voss 2022년 1월 16일
Anyway, I thought the premise of the question was that the data only contained integers between 1 and 5, in which case finding elements exactly equal to 1 is all fine and good. This data set obviously does not conform to that premise, so it's not clear what you want the code to do in this case... maybe round each element to the nearest integer first?
SAC CSA
SAC CSA 2022년 1월 16일
편집: SAC CSA 2022년 1월 16일
@Benjamin, thank you again. Now it's clear to me that the matrix elements are close to 1. In fact, the original question was about painting 1. But since the topic of painting a value close to 1 is related, I thought we could cover it here. I will edit the original question. So I'd like to paint the elemnts of r that is closed to 1 of red pixel. How to do it?
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 2022년 1월 16일
@Benjamin, is perfect. Thank you so much for your kindness.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

KSSV
KSSV 2022년 1월 16일

0 개 추천

This is a 3D data, read about slice.

카테고리

도움말 센터File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

질문:

2022년 1월 16일

댓글:

2022년 1월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by