Breaking legend into several parts and moving parts as necessary to prevent overlapping graphed data.
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
How do I automatically check to see if the legend is covering plotted data.If the legend isn't covering any data than do nothing. Otherwise, break up the legand and start placing las much egend information as possible in the northest, then northwest, then southwest, and finally in the southeast.

fig=figure('Name',['dq0 variables: powers & load torque = ' loadtorque ' (N∙m)']);
fig.Position = [300 300 360 220]; % [left, bottom, width, height]
hold on;
plot(pin_dq0/1000);
plot(pqd0s_dq0/1000);
plot(pqd0r_dq0/1000);
plot(pshaft_dq0/1000);
hold off;
title('');
ylim([-75 225])
yticks(-50:50:250);
xlabel('Time (s)',Fontname='Times New Roman',fontsize=8);
ylabel('Power (kW)',Fontname='Times New Roman',fontsize=8);
legend('P_{in} apparent' , ...
'P_{stator} losses', ...
'P_{rotor} losses referred', ...
'P_{shaft}', ...
fontname='Times New Roman',fontsize=8);
box off;
grid on;
0 comentarios
Respuestas (2)
Star Strider
el 23 de Abr. de 2025
Editada: Star Strider
el 29 de Abr. de 2025
I doubt that it is possible to break up the legend or have more than one legend in an axes object.
There are a few options you could pursue:
One could be:
lgd = legend ( ... )
lgd.NumColumns = 2;
lgd.Location = 'northoutside';
Another could be:
legend( ..., Location='eastoutside')
And other variations.
See the legend documentation for details.
EDIT — (29 Apr 2025 at 19:44)
Fs = 1000;
L = 2;
t = linspace(0, Fs*L, Fs*L+1).'/Fs;
PkW = sin(t*(10:10:40)*2*pi) + 1+(0:3)*4;
figure
plot(t, PkW)
grid
xl = xlim;
yl = ylim;
Ax = gca;
Ax.OuterPosition = Ax.OuterPosition .* [0.9 1 0.85 1];
pos = Ax.InnerPosition;
xapf = @(x,pos,xl) pos(3)*(x-min(xl))/diff(xl)+pos(1); % 'x' Annotation Position Function
yapf = @(y,pos,yl) pos(4)*(y-min(yl))/diff(yl)+pos(2); % 'y' Annotation Position Function
endvals = PkW(end,:);
for k = 1:size(PkW,2)
annotation('arrow', xapf([2.2 2], pos,xl), yapf([1 1]*endvals(k),pos,yl))
% Q = [k xapf(2.2,pos,xl), yapf(endvals(k)-0.5,pos,yl), xapf(0.5,pos,xl), yapf(1,pos,yl)]
annotation('textbox', [xapf(2.2,pos,xl), yapf(endvals(k)-2.35,pos,yl), xapf(0.5,pos,xl), yapf(1,pos,yl)], 'String',sprintf('Column %d',k), 'FitBoxToText',1)
end
If I had your data, I could probably write specific code for it using this approach. (I wrote the 'xapf' and 'yapf' functions myself to make the absolute reference calculations easier.) You can probably also use text objects for this.
0 comentarios
Thorsten
el 28 de Abr. de 2025
You can check if the legend covers plotted data using the function proposed here: https://de.mathworks.com/matlabcentral/answers/448649-how-to-place-a-legend-in-best-corner
If there is a clash I would increase the y-axis and replot the legend such that the legend does not overlap the data.
0 comentarios
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!