 Dear Octavers,

I have to represent three functions: the first is x^2, the second 2*x+3 and the third x/2. The x-axis is from 1 to 10. I would like to put the parts of legend corresponding to the first and the third functions in the northwest and the part corresponding to the second function in the north.
How can I proceed?
Many thanks

George

I don’t know if there is a canonical way to do this. I think one axis object can have only one legend.

So a hack is to make two overlapping axes. Make the top one transparent. plot the two plots on one axis and one plot on the other axis. This is not an ideal method by any measure.

``````hn1 = axes()
% plot one set of lines
plot(cumsum(rand(6,1)+1), rand(6,1));

hn2 = axes()

% plot next set of lines
plot(cumsum(rand(8,1)+1), rand(8,1));

% make second plot background transparent
set(hn2, 'color', 'none')

% explicitly select and set the legend
legend(hn2, '2');
legend(hn1, '1', 'location', 'north');

``````

Note that, since the lines are plotted in different axes, the colour turnsout t be the same. So you have to set the line colour also explicitly while plotting.

It also seems that any action on the top axis resets the background color. So I gave the `set(hn2, 'color', 'none')` after plotting the lines.

This is correct. There is one legend object per axes, so if you want two legends then you need two axes.

The `plot` command automatically resets the `axes` object unless `hold on` has been called. This is why it makes sense to do all adjustments to the axes after they have been created and populated with graphics objects. One additional step you will want to take is to lock the axes X and Y limits to the same values so the tickmarks show in the same places. The code below should do what the original poster (@george_brida) was looking for.

``````## Create data for plots
x = linspace (1,10, 50);
y1 = x.^2;
y2 = 2*x + 3;
y3 = x / 2;

## Create plot figure and background axes
hf = figure ();
hax1 = axes ();
hax2 = axes ();

## Plot data, use explicit colors
plot (hax1, x,y1,'r', x,y3, 'g');
plot (hax2, x,y2,'b');

## Make plot look pretty
axis (hax1, [1, 10, 0, 100]);
axis (hax2, [1, 10, 0, 100]);
set (hax2, 'color', 'none');