legend showing wrong colours
15 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Robin Strak
am 17 Mär. 2020
Kommentiert: Adam Danz
am 18 Mär. 2020
Hi,
I want to plot 4 different arrays and their corresponding trendlines. In order to discriminate between those 4 I want them to have their own colour. Unfortunately my legend doesn´t correspond to the plots (see attached screenshot) - it should only show "Graph A1", "Graph A2", "Graph A3" and Graph A4" with four different colours.
j = 1;
k = 1;
str = {'A1', 'A2', 'A3', 'A4'};
col = {'r', 'k', 'g', 'b'};
for i = 1:20:80
plot(M_3(i:19+i), col{k}, 'LineWidth',1);
str = [str ("Graph " + str(j))];
c_rms = polyfit(time,M_3(i:19+i),1);
y_est = polyval(c_rms,time);
hold on
plot(time,y_est, col{k}, 'LineWidth',2);
j = j+1;
k = k+1;
end
set(gcf,'position',[0 500 1000 300])
xlabel('time / min');
ylabel('electrical activity in % of the maximal force');
grid on; grid minor;
legend(str);
I would be very happy if you could help me out, maybe I just missordered the loop.
Thanks,
Robin
0 Kommentare
Akzeptierte Antwort
Adam Danz
am 17 Mär. 2020
Bearbeitet: Adam Danz
am 17 Mär. 2020
The easiest way to manage legend text is by using the DisplayName property of graphics objects. Here's how that might look when plotting in a loop.
str = {'A1', 'A2', 'A3', 'A4'};
hold on
for i = 1:4
% . . . skipping stuff
plot(x,y,'DisplayName', str{i})
plot(xFit,yFit,'DisplayName', [str{i},' fit'])
end
legend()
If you only want some objects to appear in the legend,
str = {'A1', 'A2', 'A3', 'A4'};
hold on
handles = gobjects(4,1);
for i = 1:4
% . . . skipping stuff
handles(i) = plot(x,y,'DisplayName', str{i});
plot(xFit,yFit)
end
legend(handles)
2 Kommentare
Adam Danz
am 18 Mär. 2020
Instead of setting up your loop like this
for i = 1:20:80
plot(M_3(i:19+i), . . .);
. . .
end
set it up like this
vec = 1:20:80;
for i = 1:numel(vec)
plot(M_3(vec(i):19+vec(i)), . . .);
% ^^^^^^ ^^^^^^
. . .
end
That way your loop uses integer values 1:n that can be used as indices.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Graphics Performance finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!