Move line in legend

I have a cell array with four strings which is used as legend for four individual X,Y plots. One string is very long and therefore devided into a four-line legend by sprntf, The four plot legend is shown in the figure below. Is it possible to move the blue line up so it fits the first line, which is next to the 'Av.'
Here is a short example of code:
X=[2 4 6 8; 2 3 4 5; 4 5 6 7 ; 7 6 8 9];
Y=[1 3 5 7; 2 5 6 8; 8 6 4 2; 7 5 4 3];
Title = {
'123456789_1'
'ABCDEFGHIJ_1'
'123ABC_1'
sprintf('Av. \n(123456789_1 \nABCDEFGHIJ_1 \n123ABC_1)')
};
fig1=figure;
hold on
for i=1:size(X,2)
plot(X(:,i),Y(:,i));
end
hold off
legend(Title,'Orientation','vertical','Location','northeastoutside','FontSize',8);

Answers (1)

To move the line in legend you can change the YData value of the legend line as shown in the below code.
x= 1:5;
y1= 2*x;
y2= 3*x;
Title = { 'legend_1' sprintf('legend_2 \nlegend_3')};
fig1=figure;
hold on
plot(x,y1);
plot(x,y2);
hold off
legend(Title,'Orientation','vertical','Location','northeastoutside','FontSize',8);
fig2=figure;
hold on
plot(x,y1);
plot(x,y2);
hold off
[lgd,icons,plots,txt] = legend(Title,'Orientation','vertical','Location','northeastoutside','FontSize',8);
icons(5).YData = [0.5 0.5];
However, the syntax used for figure 2 legend is not recommended as it creates a legend that does not support all graphics features.

Products

Tags

Asked:

JB
on 11 Oct 2017

Answered:

on 13 Oct 2017

Community Treasure Hunt

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

Start Hunting!