Plotting a legend without displaying data on UIAxes

10 views (last 30 days)
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 Comments
Walter Roberson
Walter Roberson on 26 Sep 2022
What legend would you like displayed when all of your lines are invisible?
Mohammad Shahbazy
Mohammad Shahbazy on 26 Sep 2022
I have the lines displayed in a different UIAxes for some reasons in my app, then here only need to have a legend.

Sign in to comment.

Accepted Answer

Chris
Chris on 26 Sep 2022
Edited: Chris on 26 Sep 2022
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

More Answers (1)

Simon Chan
Simon Chan on 26 Sep 2022
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');

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!