Move line in legend

4 views (last 30 days)
JB
JB on 11 Oct 2017
Answered: Janak Thotakura on 13 Oct 2017
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)

Janak Thotakura
Janak Thotakura on 13 Oct 2017
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.

Tags

Products

Community Treasure Hunt

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

Start Hunting!