Borrar filtros
Borrar filtros

How to remove a default generated legend item from a figure plot of MATLAB?

11 visualizaciones (últimos 30 días)
Hi All,
I tried to remove one of the legend item (data 1) as shown below which had been generated default using the legend box icon on the figure pallete, however, it was unsuccessful to do that.
Please could you let me know how this can be done retaining the rest of the legend items, deleting data 1 legend from the legend bar.
Thanks in advance and looking forward to hear from you soon.
% Read the excel file using readtable function
rawTable = readtable('Final Filtered Catalogued Object Data - Copy.xlsx','Sheet','Final Data');
x = rawTable.ApogeeAltitude; %: get the excel column, ApogeeAltitude(Km) (header name)
y1 = rawTable.RocketBody; %: get the excel column, RocketBody (header name)
y2 = rawTable.Debris; %: get the excel column, Debris (header name)
y3 = rawTable.Payload; %: get the excel column, Payload (header name)
figure;
s=stairs(x,y1);
set(gca, 'ylim', [0 55],'TickLabelInterpreter','latex');
set(gcf,'color','w');
hold on
p1 = xline(590,'r');
hold on
p2 = xline(610,'b');
hold on
p3 = xline(630,'color',[0.4660 0.6740 0.1880]);
hold on
BB = min(s.YData);
X = [s.XData(1),repelem(s.XData(2:end),2)];
Y = [repelem(s.YData(1:end-1),2),s.YData(end)];
fill([X,fliplr(X)],[Y,BB*ones(size(Y))], [0.65 0.65 0.65]);
hold off
xlabel('Apogee Altitude (Km)','Interpreter','latex');
ylabel('Number of Catalogued Objects','Interpreter','latex');
  4 comentarios
Dyuman Joshi
Dyuman Joshi el 14 de Ag. de 2023
Can you attach your data? The excel file? Use the paperclip button to attach.

Iniciar sesión para comentar.

Respuesta aceptada

Voss
Voss el 14 de Ag. de 2023
One way to prevent the 'data1' line from appearing in the legend is to set its 'HandleVisibility' to 'off'. See below.
unzip('Final Filtered Catalogued Object Data - Copy.zip')
% Read the excel file using readtable function
rawTable = readtable('Final Filtered Catalogued Object Data - Copy.xlsx','Sheet','Final Data');
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
x = rawTable.ApogeeAltitude; %: get the excel column, ApogeeAltitude(Km) (header name)
y1 = rawTable.RocketBody; %: get the excel column, RocketBody (header name)
y2 = rawTable.Debris; %: get the excel column, Debris (header name)
y3 = rawTable.Payload; %: get the excel column, Payload (header name)
figure;
s=stairs(x,y1,'HandleVisibility','off');
set(gca, 'ylim', [0 55],'TickLabelInterpreter','latex');
set(gcf,'color','w');
hold on
p1 = xline(590,'r');
p2 = xline(610,'b');
p3 = xline(630,'color',[0.4660 0.6740 0.1880]);
BB = min(s.YData);
X = [s.XData(1),repelem(s.XData(2:end),2)];
Y = [repelem(s.YData(1:end-1),2),s.YData(end)];
fill([X,fliplr(X)],[Y,BB*ones(size(Y))], [0.65 0.65 0.65]);
hold off
xlabel('Apogee Altitude (Km)','Interpreter','latex');
ylabel('Number of Catalogued Objects','Interpreter','latex');
legend({'590 Km Altitude','610 Km Altitude','630 Km Altitude','Rocket Body'},'EdgeColor','none')
  3 comentarios
Rufiya
Rufiya el 14 de Ag. de 2023
Dear Voss,
Thanks a lot for the quick help ! Your code worked for me.

Iniciar sesión para comentar.

Más respuestas (1)

phenan08
phenan08 el 14 de Ag. de 2023
You should try the follwing code:
l = legend(gca) ; % gets the legend object of the current axes
l.Visible = "off" ;
  1 comentario
Rufiya
Rufiya el 14 de Ag. de 2023
Hello,
I tried adding the above code to my script and unfortunately it did not result giving any legend output on my graph. If possible, could you suggest any other alternative way to resolve this issue?

Iniciar sesión para comentar.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by