How to remove a default generated legend item from a figure plot of MATLAB?
15 views (last 30 days)
Show older comments
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 Comments
Dyuman Joshi
on 14 Aug 2023
Can you attach your data? The excel file? Use the paperclip button to attach.
Accepted Answer
Voss
on 14 Aug 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');
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 Comments
More Answers (1)
phenan08
on 14 Aug 2023
You should try the follwing code:
l = legend(gca) ; % gets the legend object of the current axes
l.Visible = "off" ;
See Also
Categories
Find more on Data Distribution Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!