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!