Patch - controlling transition of transparency - how to?

I made a plot where the patch background goes from a specific color to full transparency (not white color).
Question is: I would like to change the transparency limits and don't know how. You can see that for some peaks the blue color goes from deep blue to full transparency. Some of them don't, and I don't like that. I want every peak to have a transition from full blue to full transparency. Or better - precisely control it from-to. How to do it please?
I'm stuck on the FaceVertexAlphaData property and have no idea exactly what to put in it and how.
Working code snippet for all blue peaks:
% fit(:,1) is for X data, fit(:,i) is for Y data; data are in a columns
for i = 2:9
patch(fit(:,1),fit(:,i),'b','edgecolor','none','FaceAlpha','interp','FaceVertexAlphaData',fit(:,i)); % background
plot(fit(:,1),fit(:,i),'Color','b'); % edges
end

 채택된 답변

Voss
Voss 2023년 3월 8일
To have all patches span the full transparency range, use normalized values, where each column of fit is normalized separately.
% some matrix, I call it fitt (fit is a built-in function)
x = (71:169).';
fitt = [x 100*exp(-(x-110).^2/10) 50*exp(-(x-135).^2/30) 20*exp(-(x-95).^2/60)];
% normalize each column of fitt by dividing by its maximum
fitt_norm = fitt./max(fitt,[],1);
figure
hold on
% fitt(:,1) is for X data, fitt(:,i) is for Y data; data are in a columns
for i = 2:size(fitt,2)
patch(fitt(:,1),fitt(:,i),'b', ...
'edgecolor','none', ...
'FaceAlpha','interp', ...
'FaceVertexAlphaData',fitt_norm(:,i)); % background
plot(fitt(:,1),fitt(:,i),'Color','b'); % edges
end

댓글 수: 5

Leo
Leo 2023년 3월 9일
편집: Leo 2023년 3월 9일
Thank you so much for the working example. This is helpful.
I'm trying to reproduce it with my data and it seems there is something wrong with it. Any ideas please?
"Working" example:
clc; clear all;
load("fit_data.mat"); % 1600x10 double
% Create figure
Raman_fig = figure;
axes1 = axes('Parent',Raman_fig);
hold(axes1,'on');
fit_color = [0 0 1];
fit_line_color = [0 0 1 0.4];
fit_norm = fit_data./(max(fit_data,[],1)); % column normalization
for i = 2:size(fit_data,2)-1 % the last column is ommited (envelope)
patch(fit_data(:,1),fit_data(:,i),fit_color,'edgecolor','none','FaceAlpha','interp','FaceVertexAlphaData',fit_norm(:,i));
plot(fit_data(:,1),fit_data(:,i),'Color',fit_line_color);
end
plot(fit_data(:,1),fit_data(:,10),'Color','k'); % envelope
set(gca, 'ylim', [500,2000], 'xlim', [70,170])
xlabel("Raman shift / cm^{−1}");
ylabel("Intensity / a.u.");
box on;
hold(axes1,'off');
Warnings I'm getting (4 times):
% Warning: Error updating Patch.
%
% Enforcing Delaunay constraints leads to unstable repeated constraint intersections.
% This may be due to near coincident intersecting constraints or near duplicate points.
Try the normalization as defined below. (The normalization in the original answer only works if the data starts at 0.)
load("fit_data.mat"); % 1600x10 double
% Create figure
Raman_fig = figure;
axes1 = axes('Parent',Raman_fig);
hold(axes1,'on');
fit_color = [0 0 1];
fit_line_color = [0 0 1 0.4];
min_fit_data = min(fit_data,[],1);
max_fit_data = max(fit_data,[],1);
fit_norm = (fit_data-min_fit_data)./(max_fit_data-min_fit_data); % column normalization
for i = 2:size(fit_data,2)-1 % the last column is ommited (envelope)
patch(fit_data(:,1),fit_data(:,i),fit_color,'edgecolor','none','FaceAlpha','interp','FaceVertexAlphaData',fit_norm(:,i));
plot(fit_data(:,1),fit_data(:,i),'Color',fit_line_color);
end
plot(fit_data(:,1),fit_data(:,10),'Color','k'); % envelope
set(gca, 'ylim', [500,2000], 'xlim', [70,170])
xlabel("Raman shift / cm^{−1}");
ylabel("Intensity / a.u.");
box on;
hold(axes1,'off');
Leo
Leo 2023년 3월 9일
편집: Leo 2023년 3월 9일
Thanks Voss, you're amazing! Very quick response.
For those who want to use my snippet, it currently does not work in R2023a prelease. Lower versions are OK.
You're welcome!
Out of curiosity, what's the problem in R2023a prerelease?
Well there were two issues - you solved the first one (transparency limits).
Second was a different thing. Since I'm switching between versions, the last image I posted is actually from the R2023a version (I got them confused). As you can see the patch just wasn't created for some peaks and instead I got the aforementioned warning (also from R2023a). I honestly have no idea what it means. It will probably be reproducible in R2023a with my last mentioned script (and also yours).
In 2022b there's no such a thing. Everything runs smoothly.

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

추가 답변 (0개)

카테고리

제품

릴리스

R2022b

질문:

Leo
2023년 3월 8일

댓글:

Leo
2023년 3월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by