Need help fixing the color of the lines
17 次查看(过去 30 天)
显示 更早的评论
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
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.
采纳的回答
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 个评论
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 Center 和 File Exchange 中查找有关 Discrete Data Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!