How to plot two matrices overlapping in the same figure with transparent color?

13 ビュー (過去 30 日間)
SAC CSA
SAC CSA 2022 年 1 月 28 日
コメント済み: SAC CSA 2022 年 1 月 30 日
Hi everyone. I have two data sets, data1.mat and data2.mat, attached. Each data consists of a 2D matrix with 0 and 1's elements. I would like to plot two matrices overlapped in the same figure. I would like to print the 0's of data2.mat by black color and 1's by blue color. In relation to data1.mat, I would like to overlaps to data2.mat and to print only 1's by transparent red color (no plot 0's pixel). If possible, the border of the region described by 1's in the data2.mat is not transparent (0% transparent).
My code is:
%Load/read data1
S1 = load('data1.mat')
C1 = struct2cell(S1);
A = cat(2,C1{:})
%Load/read data2
S2 = load('data2.mat')
C2 = struct2cell(S2);
B = cat(2,C2{:})
%Define Abscissa and ordinate
x=linspace(-0.5*2*pi+2*pi/128,0.5*2*pi,128);
y=linspace(-0.5*2*pi+2*pi/128,0.5*2*pi,128);
%Plot Figures
dd= figure('visible','off'); clf;
%plot first data
cmap1 = [0 0.4470 0.7410; 0 0 0];
im1 = imagesc(x,y,B);
colormap(cmap1);
hold all;
%plot second data
cmap2 = [1 0 0; 1 1 1];
im2 = imagesc(x,y,A);
colormap(cmap2);
im2.AlphaData = 0.8;
xlabel('x','Interpreter','Latex','FontSize',22);
ylabel('y','Interpreter','Latex','FontSize',22);
set(gca, 'XDir', 'normal', 'YDir', 'normal');
But it does not work
Can anybody help me? Thank you very much in advance

採用された回答

Voss
Voss 2022 年 1 月 28 日
編集済み: Voss 2022 年 1 月 28 日
Here are two methods to do it (the second one is probably easier).
Step 0, load data and prepare the variables:
%Load/read data1
S1 = load('data1.mat')
S1 = struct with fields:
mask1: [128×128 double]
C1 = struct2cell(S1);
A = cat(2,C1{:})
A = 128×128
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
%Define Abscissa and ordinate
x=linspace(-0.5*2*pi+2*pi/128,0.5*2*pi,128);
y=linspace(-0.5*2*pi+2*pi/128,0.5*2*pi,128);
Now, the first method: Don't use the axes 'Colormap' property. Instead, explicitly state each image's CData as a 3D matrix (128-by-128-by-3) true-color RGB values:
%Plot Figures
% Let's use a visible figure so we can see what we're doing:
dd= figure('visible','on'); clf;
% Make the first row of cmap1 (black) correspond to B == 0,
% and second row (blue) to B == 1:
cmap1 = [0 0 0; 0 0.4470 0.7410];
% create 3-D matrix (128-by-128-by-3) of RGB true-color values
% B_cdata to explicitly state the colors of im1, rather than using
% the axes 'Colormap' (because the other image, im2, would use a
% different Colormap, but this is impossible since they are in the
% same axes)
B_cdata = reshape(cmap1(B+1,:),[size(B) 3]);
im1 = image(x,y,B_cdata);
hold all
% Make the first row of cmap2 (white) correspond to A == 0,
% and second row (red) to A == 1:
cmap2 = [1 1 1; 1 0 0];
% RGB truecolor matrix for im2:
A_cdata = reshape(cmap2(A+1,:),[size(A) 3]);
im2 = image(x,y,A_cdata);
% now use matrix AlphaData:
% AlphaData = 0 where A is 0;
% AlphaData = 0.5 where A is 1:
% This makes the white pixels (where A is 0) completely transparent
% and the red pixels (where A is 0) have 0.5 transparency/opacity
im2.AlphaData = 0.5*A;
xlabel('x','Interpreter','Latex','FontSize',22);
ylabel('y','Interpreter','Latex','FontSize',22);
% not necessary:
% set(gca, 'XDir', 'normal', 'YDir', 'normal');
Second method: Do use the axes 'Colormap' property, using a combined colormap for the two images, with four colors; index into the combined colormap appropriately for each image:
dd= figure('visible','on'); clf;
% four-color colormap:
% black, blue, white, red corresponding to B==0, B==1, A==0, A==1,
% respectively:
cmap1 = [0 0 0; 0 0.4470 0.7410; 1 1 1; 1 0 0];
% set the colormap to the axes:
colormap(cmap1);
% create the first image:
% B==0 maps to row 1 of colormap
% B==1 maps to row 2 of colormap
im1 = image(x,y,B+1);
hold all
% create the second image:
% A==0 maps to row 3 of colormap
% A==1 maps to row 4 of colormap
im2 = image(x,y,A+3);
% same AlphaData as before
im2.AlphaData = 0.5*A;
xlabel('x','Interpreter','Latex','FontSize',22);
ylabel('y','Interpreter','Latex','FontSize',22);
% not necessary:
% set(gca, 'XDir', 'normal', 'YDir', 'normal');

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeColormaps についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by