Colored gradient fill under curve
42 views (last 30 days)
Show older comments
I know this is not the most elegant solution, but it works. Indeed, I am a beginner, so excuse me. My question is, how to simplify it all?
Here's what I want to achieve: I have a 2D plot and I want to make a gradient fill under curve. From one specific color to transparent. I tried to use white instead of transparent, but I want the grid to be visible. So I'm little lost here.
Did some search and I looked for some examples here and tried to modify them. So, this is an edited code that worked to fill a specific range. I spread it all over the axis. I also didn't want to fill the gradient over the edge of the curve, so I had to insert plot again. Yeah, that's not ideal at all, but as I said, it's working.
I believe that this can be done more easily. Could you advise me, please?
% Input data
x = 0:0.01:10*pi;
y1 = sin(x)+9;
y2 = repmat(5,length(y1),1);
figure1 = figure;
plot(x,y1);
grid on;
hold on;
% Fill the whole area - 0-31.4
val = [0,31.4];
for i = 1:2;
tmp = abs(x-val(i));
[~,idx(i)] = min(tmp);
end
id = idx(1):1:idx(2);
x2 = x(id);
y1a = y1(id);
y2a = y2(id);
y2a = y2a';
X=[x2,fliplr(x2)];
Y=[y1a,fliplr(y2a)];
% I set the transparency and removed borders.
% This is a workaround, I don't want to set the whole transparency.
h = fill(X,Y,Y,'LineStyle','none');
set(h,'facealpha',.5)
% Here is how I made the gradient between red and white.
% This is not very nice solution and I would like to simplify it.
% Of course, color bar is not visible.
set(gca,'Colormap',[1 1 1; '...a lot of data...' ;1 0 0]);
% Need to plot again because of overlaping
plot(x,y1);
0 Comments
Accepted Answer
darova
on 28 Jun 2020
Use alphaData property, set facealpha to interp
x = 0:0.2:10;
y = sin(x)+10;
xx = [x;x];
yy = [y;y*0];
surf(xx,yy,xx*0,...
'alphadata',yy,...
'facealpha','interp',...
'edgecolor','none');
line([0 x x(end) 0],[0 y 0 0],'linew',2)
view(2)
More Answers (0)
See Also
Categories
Find more on Lighting, Transparency, and Shading 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!