%% Legend Location and Number of Columns
% Plot four lines. Create a legend in the northwest area of the axes. Specify the number of legend columns using the NumColumns property.

x = linspace(0,pi);
y1 = cos(x);
plot(x,y1)

hold on
y2 = cos(2*x);
plot(x,y2)

y3 = cos(3*x);
plot(x,y3)

y4 = cos(4*x);
plot(x,y4)
hold off

legend({'cos(x)','cos(2x)','cos(3x)','cos(4x)'},...
    'Location','northwest','NumColumns',2)

% By default, the legend orders the items from top to bottom along each column. To order the items from left to right along each row instead, set the Orientation property to 'horizontal'.
% Copyright 2012 The MathWorks, Inc.