How to display only one legend for a figure with multiple plots?

조회 수: 267 (최근 30일)
Armel
Armel 2013년 6월 10일
댓글: Adam Danz 2022년 9월 16일
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

답변 (5개)

Camilo Malagon Nieto
Camilo Malagon Nieto 2018년 4월 24일
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')
  댓글 수: 2
hamid rafiei
hamid rafiei 2022년 9월 7일
Dear
thanks for your answer
if I have a figure that saved as " .fig", how can I display one unique legend for a figure with multiple plots?
Thanks again

댓글을 달려면 로그인하십시오.


Raghu S
Raghu S 2018년 8월 30일
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.

Angus
Angus 2013년 6월 10일
"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.)."
  댓글 수: 3
Armel
Armel 2013년 6월 12일
편집: Armel 2013년 6월 12일
In fact, here is a similar image that I would like to produce https://docs.google.com/file/d/0BwjZP-sfazLMS3JTVVJUTkdzNEE/edit?usp=sharing 2 (or many) images with only one legend but the images are raster data.
Angus
Angus 2013년 6월 12일
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.

댓글을 달려면 로그인하십시오.


Armel
Armel 2013년 6월 11일
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
  댓글 수: 1
Angus
Angus 2013년 6월 11일
Hey, I am actually still new here as well. I am not sure if there is a standard method for linking images but I have seen some people upload via other websites like TinyPic.
Cheers, Angus.

댓글을 달려면 로그인하십시오.


Adam Danz
Adam Danz 2020년 9월 29일
이동: Adam Danz 2022년 9월 16일
Update
Starting in Matlab r2020b, legends can be positioned relative to figure edges and can contain graphics objects from different subplots created by TiledLayout.
Examples:

카테고리

Help CenterFile Exchange에서 Legend에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by