Plot line with custom color
25 views (last 30 days)
Show older comments
Hi,
How to plot a line with custom color in matlab?
I want to use the color [0 0.5 0]
Normally, I would plot it as: plot(x, y, '-g', 'LineWidth', 1) but not sure if this is the right syntax for what I want:
plot(x, y, -[0 0.5 0], 'LineWidth', 1)
0 Comments
Accepted Answer
Les Beckham
on 3 May 2023
Edited: Les Beckham
on 3 May 2023
This looks a bit jaggedy and dark here, but looks good in my desktop Matlab.
x = linspace(0, 4*pi);
y = sin(x);
plot(x, y, 'Color', [0 0.5 0], 'LineWidth', 1)
grid on
3 Comments
Les Beckham
on 3 May 2023
Plot the markers and the line at the same time. See below.
I'm not sure why the lines don't seem to be where I would expect them to be, but that isn't what you were asking about.
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');
grid on
box on
hold on
% Define markers
Markers = ['*s+'];
Colors = { 'r', 'b', [0 0.5 0]};
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);
if currentStart ~= currentEnd
x = [currentStart currentEnd];
if Count(i) < minCount(i)
% plot(currentEnd, currentVar1, Marker1, 'Color', 'b')
% plot(currentEnd, currentVar2, Marker2, 'Color', 'b')
% plot(currentEnd, currentVar3, Marker3, 'Color', 'b')
plot(x, [currentVar1 currentVar1], 'Color', Colors{1}, 'Marker', Markers(1), 'LineWidth', 1)
plot(x, [currentVar2 currentVar2], 'Color', Colors{2}, 'Marker', Markers(2), 'LineWidth', 1)
plot(x, [currentVar3 currentVar3], 'Color', Colors{3}, 'Marker', Markers(2), 'LineWidth', 1)
end
end
end
grid on
currentVar1
currentVar2
currentVar3
More Answers (0)
See Also
Categories
Find more on Line Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!