Plot line with custom color
22 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Les Beckham
2023-5-3
编辑:Les Beckham
2023-5-3
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 个评论
Les Beckham
2023-5-3
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Line Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!