필터 지우기
필터 지우기

Same color lines in legend using a for loop

조회 수: 9 (최근 30일)
Noah
Noah 2022년 9월 20일
댓글: Mathieu NOE 2023년 2월 12일
When I try and run this code, all the lines in the legend show up as red lines instead of different colors.
hold on
for i = 1:length(g)
if g(i) >= 2
plot(x,y,Color='b');
else
plot(x2,y2,Color='r');
end
end
hold off
hold on
plot(x2,y2,Color='y');
plot(x3,x3,,Color='m');
hold off
legend('l1','l2','l3','l4')
  댓글 수: 1
Dyuman Joshi
Dyuman Joshi 2022년 9월 20일
1 - Do not use equal-to "=" sign between 'Color', and color value, inside the plot() function.
Use a comma ","
2 - Using "hold on" immediately after "hold off" seems a bit redundant.
3 - You might be overwriting the x2-y2 curve, color 'r' with itself x2-y2 curve, color 'y'. In the case, you would only find 3 curves
Other than this, I can not say what the error is (if it occurs after considering above points), data for the variables will be required to make any more comments on it.

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

답변 (1개)

Mathieu NOE
Mathieu NOE 2022년 9월 21일
hello
why make simple things complicated ?
if you have only 4 arrays to plot , you could do directly the plot like this - and the colors appears correctly in the legend box
% dummy data
x = 1:5;
y = 2*x+1;
x2 = 1.5*x;
y2 = 2*y+1;
x3 = 0.5*x;
% plot
figure
plot(x,y,'b',x2,y2,'r',x2+1,y2+1,'y',x3,x3,'m');
legend('l1','l2','l3','l4')
if you want a for loop and assign a color / line style different to each line , you can do this :
t = 0:pi/50:2*pi;
MarkerStyle={'o','+','*','x','s','d','v','>'};
Colors=[1 0 0; 1 0 1; 0 1 0; 0.4660 0.6740 0.1880; 0 0 1; 0.3010 0.7450 0.9330; 0.8500 0.3250 0.0980; 0.9290 0.6940 0.1250]
figure(1),hold on
for ck = 1:8
p = plot(t,sin((1+0.2*ck)*t),'LineWidth',2,'Color',Colors(ck,:),'Marker',MarkerStyle{ck});
leg_str{ck} = (['Line #' num2str(ck)]);
end
legend(leg_str);
  댓글 수: 3
Sophia Ehlers
Sophia Ehlers 2023년 2월 11일
This does'nt work for me, is it possible that this only works for plots but not for error bars? Or what other possible mistakes have i made?
color = [0.933 0.443 0.0; 0.0 0.30588 0.62351; 0.0 0.6588 0.475; 161./255 217./255 248./255; 0./255 120./255 120./255];
Legends={'10,2 °C'; '15 °C'; '21,8 °C'; '26 °C'; '30 °C'};
%get multiple sets of data
for file_j = 1:5
file = strcat('Kennlinien', '_', num2str(file_j), '.txt');
data = load(file,'txt');
U = data(:,1); % Voltage Data
I = data(:,2); % Current Data
%errors
errU = 0.01.*ones(size(U));
errI = 0.01.*ones(size(I));
%plot
e = errorbar(I ,U , errU, errU, errI, errI, 'o', 'Color', color(file_j,:),'Marker','o', 'MarkerSize',6);
Legend{file_j}=(['T_' num2str(file_j) '=' Legends{file_j}]);
end
Error using load
Unable to find file or directory 'Kennlinien_1.txt'.
legend(Legend,'Location', 'Northwest');
Mathieu NOE
Mathieu NOE 2023년 2월 12일
hello
seems that the file "Kennlinien_1.txt" is simply not present in your current directory
please double check
dir *.txt
all the best

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

카테고리

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

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by