Hello everyone,

I have two questions:

1) How can I display one unique legend for a figure with multiple plots?

2) Let assume 5,4,3, 2 and 1 as code for "very high", "High", "Moderate", "Low", and "Very Low" the values of my variable of interest. How can I display these characters strings instead of the value 5,4,3,2, and 1 in the legend?

Any thoughts for a code will be very much appreciated.

Armel

Answer by Raghu S
on 30 Aug 2018

Here is the answer: menation these line inside legend command 'orientation','horizontal'

example: legend('orientation','horizontal','name1','name2','name3')

some example:

____________Mutiple legend:_______________

t=0:0.001:1;

sig1=2*sin(2*pi*10*t);

sig2=2*cos(2*pi*10*t);

subplot 212;plot(sig1);hold on;plot(sig2);legend('sin','cos')

subplot 211;plot(sig1);hold on;plot(sig2);legend('sin','cos')

____________Single legend___________________

legend('Location','bestoutside','orientation','horizontal','sin','cos')

One can move anywhere from the figure editor.

Attached one more example.

Sign in to comment.

Answer by Angus
on 10 Jun 2013

"legend('string1','string2',...) displays a legend in the current axes using the specified strings to label each set of data." ~ Matlab docs.

This is the simplest way of assigning strings to the legend. The docs show other uses.

When you say you have multiple plots, do you mean multiple axis or just multiple data plots within one axis? If you have multiple axis then you can create a unique legend that specifies what you want by using the second method from the docs:

"legend(h,'string1','string2',...) displays a legend on the plot containing the objects identified by the handles in the vector h and uses the specified strings to label the corresponding graphics object (line, barseries, etc.)."

Angus
on 12 Jun 2013

Here is a crude example that I think illustrates the method.

Basically you need to get the handles for whichever objects you want in your legend, this can by done multiple ways and what I used is definitely not the best, just easy this time; you may want to use findobj(...) to get the handles if need be. Then choose a location for your legend and pass it the object handles and assign the strings. It is also possible to tidy things up by preparing the strings in an array, there are options for these things in legend().

x = [1:100];

y1 = sin(x);

y2 = 2*sin(x./2);

y3 = [1:100];

y4 = [100:-1:1];

subplot(121)

plot(x,y1,'b',x,y2,'r')

a = get(gca,'Children')

subplot(122)

plot(x,y3,'g',x,y4,'k')

b = get(gca,'Children')

h = [b;a]

legend(h,'decreasing','increasing','sine2','sine1','location','eastoutside')

Armel
on 12 Jun 2013

Angus
on 12 Jun 2013

Yes, I saw that before, I'm not sure if raster data changes the method used. I'm not sure how raster data is dealt with in matlab, I dont use it. If you can get it to plot as in my example (convert it to a vector?) then this method should work for you.

Have I answered the original question about legends and multiple plots? Sorry I cant be much help with raster data use.

Cheers, Angus.

Sign in to comment.

Answer by Armel
on 11 Jun 2013

Hi Angus,

Thanks for your reply.

Is there a way to upload a similar figure so you will be able to understand what I mean?

Armel

P.S I'm new to this community

Answer by Camilo Malagon Nieto
on 24 Apr 2018

I got a better solution that the one with b = get(gca,'Children')

From MatLab Documentation under Legend>Specify Charts to Include in Legend

x = linspace(0,3*pi); y1 = sin(x); p1 = plot(x,y1);

hold on y2 = sin(x - pi/4); p2 = plot(x,y2);

y3 = sin(x - pi/2); p3 = plot(x,y3); hold off

legend([p1 p3],'First','Third')

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 0 Comments

Sign in to comment.