Same color lines in legend using a for loop
20 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentario
Dyuman Joshi
el 20 de Sept. de 2022
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.
Respuestas (1)
Mathieu NOE
el 21 de Sept. de 2022
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 comentarios
Sophia Ehlers
el 11 de Feb. de 2023
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
legend(Legend,'Location', 'Northwest');
Mathieu NOE
el 12 de Feb. de 2023
hello
seems that the file "Kennlinien_1.txt" is simply not present in your current directory
please double check
dir *.txt
all the best
Ver también
Categorías
Más información sobre Legend en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!