How to plot one curve and change color according to value

8 Ansichten (letzte 30 Tage)
Manitux
Manitux am 18 Aug. 2024
Kommentiert: Manitux am 19 Aug. 2024
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?
  1 Kommentar
Manitux
Manitux am 18 Aug. 2024
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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

DGM
DGM am 18 Aug. 2024
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 Kommentare
Image Analyst
Image Analyst am 19 Aug. 2024
Bearbeitet: Image Analyst am 19 Aug. 2024
I don't understand why we need both m and c and c0. Isn't it just y > 25, like
m = y > 25
Manitux
Manitux am 19 Aug. 2024
Try this:
x = linspace(0,10);
y = randi([-50 50], 1, length(x));
m(length(x)) = 0;
m([30 31 32 33 34 35 36 37 60 61 62 63 64 65 90 91 92 93 94 95]) = 1;
patch([x NaN],[y NaN],[m NaN],'EdgeColor','interp');
CT = [0 0 1; 1 0 0];
colormap(CT)
"y" and "m" are signals I receive on an interface and need to paint "y" in 2 different colors, depending on the bool of "m". My first sample was just how I tried to get this solved (by myself).
But your idea to take "patch" is brilliant ;-)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (3)

Image Analyst
Image Analyst am 18 Aug. 2024
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
John D'Errico am 18 Aug. 2024
Bearbeitet: John D'Errico am 18 Aug. 2024
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

William Rose
William Rose am 18 Aug. 2024
Bearbeitet: William Rose am 18 Aug. 2024
[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 Kommentar
William Rose
William Rose am 18 Aug. 2024
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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Data Distribution Plots finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by