Plot line with custom color

조회 수: 22 (최근 30일)
MattC
MattC 2023년 5월 3일
댓글: Les Beckham 2023년 5월 3일
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)

채택된 답변

Les Beckham
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.
If you had read the documentation for plot, you could have figured this out yourself.
x = linspace(0, 4*pi);
y = sin(x);
plot(x, y, 'Color', [0 0.5 0], 'LineWidth', 1)
grid on
  댓글 수: 3
MattC
MattC 2023년 5월 3일
Thank you @Les Beckham. Is it possible to add markers in the starting and ending of a line?
I am trying to modify it in a way I can add marker in front of the line as well, like it is currently towards the end of line.
My code looks something like:
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
Marker1 = '*';
Marker2 = 's';
Marker3 = '+';
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], '-b', 'LineWidth', 1)
plot(x, [currentVar2 currentVar2], '-b', 'LineWidth', 1)
plot(x, [currentVar3 currentVar3], '-b', 'LineWidth', 1)
end
end
end
Les Beckham
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
currentVar1 = 0.1000
currentVar2
currentVar2 = 0.2000
currentVar3
currentVar3 = 1.7000

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

태그

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by