필터 지우기
필터 지우기

Need help fixing the color of the lines

조회 수: 11 (최근 30일)
MattC
MattC 2023년 4월 16일
댓글: Image Analyst 2023년 5월 3일
Hi All, I have this code which is plotting what I need but I am having issues fixing the color of lines.
Currently, see the code below, all the horizontal lines before the marker are coming in red. However, I need the color of the lines similar to the color of marker at the end: red, green, blue.
Please help!
% Define inputs
Start = ['230315';'230321';'230324'];
End = ['230320';'230323';'230324'];
Var1 = [0.2;0.2;0.1];
Var2 = [0.2;0.3;0.2];
Var3 = [0.3;0.3;1.7];
Count = [23;65;24];
minCount = [24;24;24];
tablex = table(Start,End,Var1,Var2,Var3,Count,minCount);
% Convert dates to datetime format
startdate_num = datenum(datetime(Start,'InputFormat','yyMMdd'));
enddate_num = datenum(datetime(End,'InputFormat','yyMMdd'));
tablex.Start_datetime = datetime(startdate_num,'ConvertFrom','datenum');
tablex.End_datetime = datetime(enddate_num,'ConvertFrom','datenum');
% Plot settings
limit = 1;
idx = (Count < minCount);
idx2 = any([tablex.Var1 tablex.Var2 tablex.Var3] > limit, 2);
grid on
box on
hold on
% Define markers
Marker1 = '*';
Marker2 = 's';
Marker3 = '+';
% Loop over each row in the table
for i = 1:size(tablex,1)
% Get current row data
currentStart = tablex.Start_datetime(i);
currentEnd = tablex.End_datetime(i);
currentVar1 = tablex.Var1(i);
currentVar2 = tablex.Var2(i);
currentVar3 = tablex.Var3(i);
% Check if start and end are same
if currentStart == currentEnd
% Plot markers
if Count(i) < minCount(i)
plot(currentEnd, currentVar1, Marker1, 'Color', 'b')
plot(currentEnd, currentVar2, Marker2, 'Color', 'b')
plot(currentEnd, currentVar3, Marker3, 'Color', 'b')
elseif any([currentVar1 currentVar2 currentVar3] > limit)
plot(currentEnd, currentVar1, Marker1, 'Color', 'r')
plot(currentEnd, currentVar2, Marker2, 'Color', 'r')
plot(currentEnd, currentVar3, Marker3, 'Color', 'r')
else
plot(currentEnd, currentVar1, Marker1, 'Color', 'g')
plot(currentEnd, currentVar2, Marker2, 'Color', 'g')
plot(currentEnd, currentVar3, Marker3, 'Color', 'g')
end
else
% Plot line
x = [currentStart currentEnd];
y = currentVar3; % Change this to choose which variable to use for y-axis
plot(x, [y y], '-r', 'LineWidth', 1)
% Plot markers
if Count(i) < minCount(i)
plot(currentEnd, currentVar1, Marker1, 'Color', 'b')
plot(currentEnd, currentVar2, Marker2, 'Color', 'b')
plot(currentEnd, currentVar3, Marker3, 'Color', 'b')
elseif any([currentVar1 currentVar2 currentVar3] > limit)
plot(currentEnd, currentVar1, Marker1, 'Color', 'r')
plot(currentEnd, currentVar2, Marker2, 'Color', 'r')
plot(currentEnd, currentVar3, Marker3, 'Color', 'r')
else
plot(currentEnd, currentVar1, Marker1, 'Color', 'g')
plot(currentEnd, currentVar2, Marker2, 'Color', 'g')
plot(currentEnd, currentVar3, Marker3, 'Color', 'g')
end
end
end
% Plotting the line for threshold
Limity = yline(1, '--r', 'LineWidth',1);
  댓글 수: 2
Walter Roberson
Walter Roberson 2023년 4월 16일
plot(x, [y y], '-r', 'LineWidth', 1)
%%
Limity = yline(1, '--r', 'LineWidth',1);
Those are the only places you plot lines, and you always plot them in red. The other plot() calls are all dealing with single points.
MattC
MattC 2023년 4월 16일
Thank you @Walter Roberson. The line I am talking about is:
I have currently plotted them in red but I could not figure out a way to fix the code that all the lines before the marker are same color as the marker and not red
plot(x, [y y], '-r', 'LineWidth', 1)

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

채택된 답변

Image Analyst
Image Analyst 2023년 4월 16일
Try moving the plot for the line inside the if blocks, like this:
% Plot line
x = [currentStart currentEnd];
y = currentVar3; % Change this to choose which variable to use for y-axis
% Plot markers
if Count(i) < minCount(i)
plot(currentEnd, currentVar1, Marker1, 'Color', 'b')
plot(currentEnd, currentVar2, Marker2, 'Color', 'b')
plot(currentEnd, currentVar3, Marker3, 'Color', 'b')
% Plot line in blue, the same color as the markers.
plot(x, [y y], '-b', 'LineWidth', 1)
elseif any([currentVar1 currentVar2 currentVar3] > limit)
plot(currentEnd, currentVar1, Marker1, 'Color', 'r')
plot(currentEnd, currentVar2, Marker2, 'Color', 'r')
plot(currentEnd, currentVar3, Marker3, 'Color', 'r')
% Plot line in red, the same color as the markers.
plot(x, [y y], '-r', 'LineWidth', 1)
else
plot(currentEnd, currentVar1, Marker1, 'Color', 'g')
plot(currentEnd, currentVar2, Marker2, 'Color', 'g')
plot(currentEnd, currentVar3, Marker3, 'Color', 'g')
% Plot line in green, the same color as the markers.
plot(x, [y y], '-g', 'LineWidth', 1)
end
  댓글 수: 4
MattC
MattC 2023년 5월 3일
Thank you @Image Analyst. How do I add the markers in front of these lines as well?
Currently what I have is a line and then the marker towards the end of it
Image Analyst
Image Analyst 2023년 5월 3일
If you're plotting a line like
plot(x, [y y], '-r', 'LineWidth', 1)
You can add a marker in the line specification string. Like if you want a spot:
plot(x, [y, y], 'r.-', 'LineWidth', 2, 'MarkerSize', 25);

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

태그

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by