Need help fixing the color of the lines

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 Comments

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.
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)

Sign in to comment.

 Accepted Answer

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 Comments

  1. how do I change the y variable here : currently it is set to y = currentVar3; only I want the y to be currentVar1 for first plot then currentVar2 for the second plot and so on
  2. Also, I trying to plot lines before all the 3 variables (currentVar1, currentVar2, currentVar3) where the values for start date and end date are different, not just across one of the variable, currently it is only plotting the line for currentVar1?
After your first plot of currentVar1, you need to call
hold on;
so the plots for 2 and 3 don't get blown away.
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
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);

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Objects in Help Center and File Exchange

Products

Release

R2020a

Tags

Community Treasure Hunt

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

Start Hunting!