I am trying to plot several groups (25 in my example) of data on one plot, with a legend representing each. Here is my code:
aa = 25;
c = hsv(n);
n = 25
figure; hold on; axis tight; grid on; box on;
for k = 1:n;
xy = c(k,:); % assign a different color to each group
a = outputs2{1,k} % my data is held in cells in a 1 x 25 cell array
if sum(a) ~= 0; % some groups of data are empty (this data was generated from the nctool
h(k) = scatter(a(:,3), a(:,1), aa, xy, 'filled') % seems to work....
name{k} = ['group ' num2str(k)]; % attempting to make a string array for the legend
end
end
legend(h, name);
But i get this error:
Error using legend (line 120)
Invalid argument. Type 'help legend' for more information.
Error in PlotOutputs2 (line 19)
legend(h, name);
Error in run (line 96)
evalin('caller', [script ';']);
The plot works, just not the legend. I've tried a hundred different variations and this is the closest i can get.
Also, and this is a different problem, but i spent some time trying to install the legappend tool this morning with no luck. That was also error prone. With that, i ran the example that comes with it and just got errors. I'm not sure what the mistake I'm making with that is.
Also today I reinstalled MATLAB just in case I had damaged the legend tool somehow.
Any help is greatly appreciated!!!!

 채택된 답변

pfb
pfb 2015년 4월 27일

1 개 추천

Not very sure, but I guess that the problem might be in some of the elements of h being zero, on account of the condition about empty plots.
May I suggest this? Just plot what you have to plot, and use the DisplayName property to set the relevant legend string
name = ['group ' num2str(k)];
h(k) = scatter(a(:,3), a(:,1), aa, xy, 'filled','DisplayName',name);
After you're done, inserting the legend
legend(gca,'show');
should show the correct thing.
Otherwise, you may modify your code so that h contains only entries corresponding to existing objects. You can find nonzero elements in h, or introduce a different counter kk that is increment only for nonempty groups...

댓글 수: 1

Josh White
Josh White 2015년 4월 27일
YES!!! that worked perfectly, thanks!!!!!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Thorsten
Thorsten 2015년 4월 27일

0 개 추천

You have to get rid of the empty entries
h(h==0) = [];
name(cellfun(@isempty, name)) = [];
before you can call
legend(h, name)

댓글 수: 3

Josh White
Josh White 2015년 4월 27일
This is a very elegant solution, and I really like where its going. I've been playing with it for the last couple hours.
You clear the empty entries in 'name', but the graphics placeholders are still in 'h', so I still get the errors.
I've been trying to figure out how to delete the graphics placeholders so that that 'h' and 'name' have the same dimensions.
Ive tried this:
for i = 1:n;
if h(i) = []
h(i) = []
end
end
and I've tried this:
h(h==0) = [];
h(cellfun(@isempty, h)) = [];
I was also playing with isempty.
but I can't seem to make it work. Do you have any more suggestions?
I really appreciate your help. The code you provided above is already a nice addition to what I know.
Thanks!
pfb
pfb 2015년 4월 28일
편집: pfb 2015년 4월 28일
Maybe, with reference to Josh's original code and Thorsten's proposal,
% get nonempty plots
ne = (h ~=0); % or else ne = ~(~h);
% leave only those in the handles
h = h(ne);
% leave only those in the cell array containing the names
name = name(ne);
% place all in the legend
legend(h,name);
But using DisplayName saves you from storing the labels in a cell array.
Thorsten
Thorsten 2015년 4월 28일
I used
h(h=0) = [];
to delete the zero entries in h; I can see no reason why it doesn't work for you. What are the values in your h?

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Annotations에 대해 자세히 알아보기

질문:

2015년 4월 27일

댓글:

2015년 4월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by