Does anyone know how to add the legend?

조회 수: 8 (최근 30일)
Pul
Pul 2021년 7월 23일
댓글: Star Strider 2021년 7월 23일
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
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.
Pul
Pul 2021년 7월 23일
I didn't want to be rude; indeed, I rewrote my question removing the caps.

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

채택된 답변

Star Strider
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 —
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.
.
  댓글 수: 4
Pul
Pul 2021년 7월 23일
Everything is clear.
Thank you very much for always being very helpful and kind!
Star Strider
Star Strider 2021년 7월 23일
My pleasure!
.

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

추가 답변 (1개)

John D'Errico
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.
  댓글 수: 1
Pul
Pul 2021년 7월 23일
It's not what I meant.
By the way, thank your your help.
P.S. you tell people of not being rude and then,in the end, you're unkind.

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

카테고리

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