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 个评论
Dyuman Joshi
2023-8-14
Can you attach your data? The excel file? Use the paperclip button to attach.
采纳的回答
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');
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')
更多回答(1 个)
phenan08
2023-8-14
You should try the follwing code:
l = legend(gca) ; % gets the legend object of the current axes
l.Visible = "off" ;
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Distribution Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!