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

15 次查看(过去 30 天)
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 个评论

请先登录,再进行评论。

采纳的回答

Voss
Voss 2023-8-14
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 个评论

请先登录,再进行评论。

更多回答(1 个)

phenan08
phenan08 2023-8-14
You should try the follwing code:
l = legend(gca) ; % gets the legend object of the current axes
l.Visible = "off" ;
  1 个评论
Rufiya
Rufiya 2023-8-14
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?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Distribution Plots 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by