Does anyone know how to add the legend?
4 次查看(过去 30 天)
显示 更早的评论
Hello everyone,
I know it's not that complicated, but I 've never added a legend.
Can anyone help me,please?
I need to put in legend the red and blue points, the green, magenta and cyan lines.
Thank you!
2 个评论
John D'Errico
2021-7-23
编辑:John D'Errico
2021-7-23
WILL YOU PLEASE STOP SHOUTING?
All caps are considered shouting online, and it tends to be thought of as rude. I would note that you have started posting your questions in all caps, though you did not do so at first. Please stop it.
And, does anyone know how to add a legend? Yes. I do.
采纳的回答
Star Strider
2021-7-23
I am not exactly sure what you want.
First, the clear all and close all calls are not necessary and make your code less efficient.
Second, thiese identify every plot call and plot them accordingly, each with a name —
load('GIULIA_MMEQ1.mat');
A=GIULIAMMEQ1.Var4;
B=str2double(A);
NEW= B * 10 * 0.35;
C=GIULIAMMEQ1.Dec1997;%array2table
C=replace(C,"';","");
C=datetime(C,'InputFormat','dd MMM yyyy'); %convert to datetime format
stackedplot(C,NEW)
title('Giulia AWS')
T=table(C,NEW,'Variablenames',{'Year' 'Height(mm)'}); %Put label
stackedplot(T,'XVariable','Year') %
%STAKES "RIALZATE" CON 2018 E 2011 NON DIVISO
% load('PALINE_GIULIA_2018_RIALZ');
load('PALINE_GIULIA_DELTA');
errorbar(T.Year(:,1),T.Var4(:,1),T.Var3(:,1),'.r','MarkerSize',6, 'DisplayName','First Red Errorbar')
hold on
plot(T.Year(:,1),[NEW(:,1), T.Var4(:,1)], 'DisplayName','Second Plot Call');
hold on
xlabel('Time');
ylabel('Height(mm)');
title('Giulia AWS and Stakes')
%error bar with different colors
errorbar(T.Year(1:3696,1),T.Var4(1:3696,1),T.Var3(1:3696,1),'.r','MarkerSize',6, 'DisplayName','Second Red Errorbar'); hold on
hold on
errorbar(T.Year(4406:4771,1),T.Var4(4406:4771,1),T.Var3(4406:4771,1),'.b','MarkerSize',6, 'DisplayName','First Blue Errorbar');
hold on
errorbar(T.Year(4772:4880,1),T.Var4(4772:4880,1),T.Var3(4772:4880,1),'.r','MarkerSize',6, 'DisplayName','Third Red Errorbar');
hold on
errorbar(T.Year(4881:7722,1),T.Var4(4881:7722,1),T.Var3(4881:7722,1),'.b','MarkerSize',6, 'DisplayName','Second Blue Errorbar');
hold on
plot(T.Year(:,1),[NEW, T.Var4(:,1)],'g', 'DisplayName','First Green Line');
xlabel('Time');
ylabel('Height(mm)');
title('Giulia');
load('DATI_MAR_ANNUALI');
load('DATA_ECM');
%GIULIA(MP) -->
% plot(T.Year(:,1),[NEW, T.Var4(:,1) T.Var5(:,1)],'g');
plot(T.Year(:,1),[NEW, T.Var4(:,1)], 'g' , 'DisplayName','Second Green Line')
plot(datetime(DATIMARannuali.Year,1,1), DATIMARannuali.SMB_mp_mm,'--co', 'MarkerEdgeColor','k','MarkerFaceColor','c', 'DisplayName','Cyan Line');
hold off
hold on
plot(datetime(DATIECMWFannuali.Year,1,1), DATIECMWFannuali.SMB_mp_mm,'m-*', 'DisplayName','Magenta Line');
%PROVE MOTIVI LINEA
plot(T.Year(:,1),[NEW, T.Var4(:,1)], 'g', 'DisplayName','Third Green Line')
plot(datetime(DATIMARannuali.Year,1,1), DATIMARannuali.SMB_mp_mm,'k-*', 'DisplayName','Black Line');
hold off
legend('Location','best')
producing this plot —
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/693729/image.png)
I have no idea which lines you wnat in the legend.
To select and identify them, first, choose the descriptions you want for the lines you want, change the strings following 'DisplayName' to the descriptions you want. and then assign a handle to them, for example:
figure
hp1 = plot(1:10, rand(1,10), '-r', 'DisplayName','First Plot Call');
hold on
hp2 = plot(11:20, rand(1,10), '-b', 'DisplayName','Second Plot Call');
hold off
legend(hp2, 'Location','best')
By providing only the handle to the second plot call, it is the only one that appears in the legend.
.
更多回答(1 个)
John D'Errico
2021-7-23
x = rand(1,5);
y1 = x + 2;
y2 = x*2;
plot(x,y1,'ro')
hold on
plot(x,y2,'bs')
legend('+2','*2')
Really, pretty basic. In fact, you would find all this in the help for legend.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Legend 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!