Does anyone know how to add the legend?

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 Comments

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.
I didn't want to be rude; indeed, I rewrote my question removing the caps.

Sign in to comment.

 Accepted Answer

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 Comments

Thank you for your help, as always!
But how can I delete the repetition in the legend of the same symbols? Because I tried to delete "Display Name..." but they still appear as "data".
While I can't see the green line in the plot.
My pleasure!
Assign handles to the plots you want in the legend, then only include those plots as a vector in the legend call. I expanded my earlier example here to clarify the way to do that.
figure
hp1 = plot(1:10, rand(1,10), 's-r', 'LineWidth',2, 'DisplayName','First Plot Call');
hold on
hp2 = plot(11:20, rand(1,10), '.-b', 'LineWidth',1.5, 'DisplayName','Second Plot Call');
hp3 = plot(21:30, rand(1,10), 'p-g', 'LineWidth',0.75, 'DisplayName','Third Plot Call');
hp4 = plot(31:40, rand(1,10), 'd-c', 'LineWidth',2.5, 'DisplayName','Fourth Plot Call');
hold off
legend([hp2, hp4], 'Location','bestoutside')
I am not certain exactly what you want in the legend, so I leave that to you to determine. I will of course help as best I can get it the way you want it. First, experiment with my code to determine what works to produce the result you want. We can work on the details afterr that.
.
Everything is clear.
Thank you very much for always being very helpful and kind!

Sign in to comment.

More Answers (1)

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 Comment

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.

Sign in to comment.

Asked:

Pul
on 23 Jul 2021

Commented:

on 23 Jul 2021

Community Treasure Hunt

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

Start Hunting!