Plotting a legend without displaying data on UIAxes
10 次查看(过去 30 天)
显示 更早的评论
Hi all,
I want to show a legend without showing the plot data on the app.UIAxes in App Designer. I written the following code but at the end it shows a legend box as an disabled legend (attached figure). How can I correct my code?
I would be apprciated if you kindly guide me.
Many thanks,
Moh
colororder = {'r','g','b','c','m'};
x = rand(100,5);
label = cellstr(num2str([1:1:size(x,2)]', 'cluster%d'));
for i=1:1:size(x,2)
axis(app.UIAxes,'off');
set(app.UIAxes,'visible','off');
f = plot(app.UIAxes,x(:,i),'Color',colororder{i});
hold(app.UIAxes,'on');set(f,'visible','off');
end
hold(app.UIAxes,'on');
set(app.UIAxes,'visible','off');
axis(app.UIAxes,'off');
hold(app.UIAxes,'on');
legend(app.UIAxes,label,'AutoUpdate','off');
2 个评论
Walter Roberson
2022-9-26
What legend would you like displayed when all of your lines are invisible?
采纳的回答
Chris
2022-9-26
编辑:Chris
2022-9-26
colororder = {'r','g','b','c','m'};
x = rand(100,5);
label = cellstr(num2str([1:1:size(x,2)]', 'cluster%d'));
% Plot a point but don't grab its handle
plot(app.UIAxes,0,0);
hold(app.UIAxes,'on')
for i=1:1:size(x,2)
% Get handles to the other plots, which are nan
f(i) = plot(app.UIAxes,NaN,NaN,'Color',colororder{i});
end
axis(app.UIAxes,'off');
legend(f,label,'AutoUpdate','off');
Adapted from answers here
更多回答(1 个)
Simon Chan
2022-9-26
Try this if you would like to show the figure and legend without showing the data.
Set the 'LineStyle' to 'none' to hide the lines.
colororder = {'r','g','b','c','m'};
x = rand(100,5);
label = cellstr(num2str([1:1:size(x,2)]', 'cluster%d'));
fig = figure;
ax = gca;
for i=1:1:size(x,2)
f = plot(ax,x(:,i),'Color',colororder{i},'LineStyle','none'); % Use LineStyle = 'none'
hold(ax,'on');
end
hold(ax,'off');
legend(ax,label,'AutoUpdate','off');
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Legend 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!