Plotting a legend without displaying data on UIAxes

9 次查看(过去 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
Walter Roberson 2022-9-26
What legend would you like displayed when all of your lines are invisible?
Mohammad Shahbazy
Mohammad Shahbazy 2022-9-26
I have the lines displayed in a different UIAxes for some reasons in my app, then here only need to have a legend.

请先登录,再进行评论。

采纳的回答

Chris
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
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 CenterFile Exchange 中查找有关 Legend 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by