Plotting a legend without displaying data on UIAxes

조회 수: 5 (최근 30일)
Mohammad Shahbazy
Mohammad Shahbazy 2022년 9월 26일
댓글: Mohammad Shahbazy 2022년 9월 26일
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