Ah, I see there is a misunderstanding. The "m" is part of the transmitted data, not a special limit. For the demo-code it was just generated as an example.
How to plot one curve and change color according to value
69 views (last 30 days)
Show older comments
Manitux
on 18 Aug 2024 at 18:03
Commented: Manitux
on 19 Aug 2024 at 19:07
The tricky thing is that I get an attribute with the values and would like to have this part in the plot in a different color:
x = linspace(0,10);
y = sin(3*x).*exp(0.5*x);
c = find(y > 25);
m(length(x)) = 0;
m(c) = 1;
figure;
plot(x, y, 'b')
Where m becomes 1 the color should be red, else blue.
Can someone find the easiest way to do this?
Accepted Answer
DGM
on 18 Aug 2024 at 19:39
Also:
% inputs
thresh = 25;
x = linspace(0,10,100);
y = sin(3*x).*exp(0.5*x);
% m is equivalent to (y>thresh), so it's entirely redundant
% adding 1 here casts the logical result to float
% and converts it to a 1-based index vector for direct
% addressing of the color table (CT)
c = (y > thresh) + 1;
% create an unfilled patch
hp = patch([x NaN],[y NaN],[c NaN],'EdgeColor','interp');
hp.LineWidth = 2; % if you want fatter lines
yline(thresh);
% apply the color
CT = [0 0 1; 1 0 0];
colormap(CT)
Of course, the color transitions where ever there are samples in the data, not necessarily exactly on y=25.
5 Comments
Image Analyst
on 19 Aug 2024 at 15:22
Edited: Image Analyst
on 19 Aug 2024 at 15:23
I don't understand why we need both m and c and c0. Isn't it just y > 25, like
m = y > 25
More Answers (3)
Image Analyst
on 18 Aug 2024 at 18:34
Maybe this:
x = linspace(0,10);
y = sin(3*x) .* exp(0.5 * x);
plot(x,y,'-b.');
hold on
yline(25, 'LineWidth', 2, 'Color', 'm');
mask = y > 25;
y1 = nan(1, numel(y));
y1(mask) = y(mask);
plot(x, y1,'-r.');
grid on;
John D'Errico
on 18 Aug 2024 at 22:07
Edited: John D'Errico
on 18 Aug 2024 at 22:12
As ifs often the case, I am far too late to the party. :) But there are often many ways to solve a problem, so I like to be able to offer an alternative. scatter is one in this case.
x = 1:100;
y = sin(x/10);
scatter(x,y,[],y > 0.5)
yline(0.5,'r')
For more complex cases, scatter can still work. And, of course, we can control the colormap used.
k = cos(x/10) > 0;
scatter(x,y,[],k)
colormap([0 1 0;0 0 1])
And finally, scatter will alllow me to segregate multiple sections on the curve, according to my choosing.
k = round(cos(x/10));
scatter(x,y,[],k)
colormap([1 0 0;0 1 0;0 0 1])
colorbar
0 Comments
William Rose
on 18 Aug 2024 at 18:25
Edited: William Rose
on 18 Aug 2024 at 18:26
[Edit: clean up code a lttle bit.]
x = linspace(0,10);
y = sin(3*x).*exp(0.5*x);
y1=zeros(size(y)); y2=y1; % initialize
for i=1:length(y)
if y(i)<=25
y1(i)=y(i);
y2(i)=NaN;
else
y1(i)=NaN;
y2(i)=y(i);
end
end
plot(x,y1,'-b.',x,y2,'-r.');
Probably not the easiest or prettiest way to do it but it works.
Another approach is to use scatter with a colormap.
1 Comment
William Rose
on 18 Aug 2024 at 18:32
x = linspace(0,10);
y = sin(3*x).*exp(0.5*x);
c=(y>25);
map=[0,0,1;1,0,0];
scatter(x,y,[],c,'filled');
colormap(gca,map);
"scatter" will not connect the points.
See Also
Categories
Find more on Data Distribution Plots 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!