필터 지우기
필터 지우기

How to plot one curve and change color according to value

조회 수: 69 (최근 30일)
Manitux
Manitux 2024년 8월 18일 18:03
댓글: Manitux 2024년 8월 19일 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?
  댓글 수: 1
Manitux
Manitux 2024년 8월 18일 19:50
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일 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
Image Analyst
Image Analyst 2024년 8월 19일 15:22
편집: Image Analyst 2024년 8월 19일 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
Manitux
Manitux 2024년 8월 19일 19:07
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일 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
John D'Errico 2024년 8월 18일 22:07
편집: John D'Errico 2024년 8월 18일 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

William Rose
William Rose 2024년 8월 18일 18:25
편집: William Rose 2024년 8월 18일 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
William Rose
William Rose 2024년 8월 18일 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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by