Effacer les filtres
Effacer les filtres

Need help fixing the color of the lines

11 vues (au cours des 30 derniers jours)
MattC
MattC le 16 Avr 2023
Commenté : Image Analyst le 3 Mai 2023
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 commentaires
Walter Roberson
Walter Roberson le 16 Avr 2023
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 le 16 Avr 2023
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)

Connectez-vous pour commenter.

Réponse acceptée

Image Analyst
Image Analyst le 16 Avr 2023
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 commentaires
MattC
MattC le 3 Mai 2023
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 le 3 Mai 2023
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);

Connectez-vous pour commenter.

Plus de réponses (0)

Tags

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by