フィルターのクリア

How to plot one curve and change color according to value

7 ビュー (過去 30 日間)
Manitux
Manitux 2024 年 8 月 18 日
コメント済み: Manitux 2024 年 8 月 19 日
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 件のコメント
Manitux
Manitux 2024 年 8 月 18 日
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.

サインインしてコメントする。

採用された回答

DGM
DGM 2024 年 8 月 18 日
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 件のコメント
Image Analyst
Image Analyst 2024 年 8 月 19 日
編集済み: Image Analyst 2024 年 8 月 19 日
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 2024 年 8 月 19 日
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 ;-)

サインインしてコメントする。

その他の回答 (3 件)

Image Analyst
Image Analyst 2024 年 8 月 18 日
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 2024 年 8 月 18 日
編集済み: John D'Errico 2024 年 8 月 18 日
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 2024 年 8 月 18 日
編集済み: William Rose 2024 年 8 月 18 日
[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 件のコメント
William Rose
William Rose 2024 年 8 月 18 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeData Distribution Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by