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
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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?
Réponse acceptée
DGM
le 18 Août 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 commentaires
Image Analyst
le 19 Août 2024
Modifié(e) : Image Analyst
le 19 Août 2024
I don't understand why we need both m and c and c0. Isn't it just y > 25, like
m = y > 25
Plus de réponses (3)
Image Analyst
le 18 Août 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
le 18 Août 2024
Modifié(e) : John D'Errico
le 18 Août 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
0 commentaires
William Rose
le 18 Août 2024
Modifié(e) : William Rose
le 18 Août 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 commentaire
William Rose
le 18 Août 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.
Voir également
Catégories
En savoir plus sur Data Distribution Plots dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!