single legend entry for all lines of plotted array?
Show older comments
Here is what I am trying to do: I have a matrix of spectra (wpsec.spec, ns x nv where ns=number of spectra) and would like to plot as two different types of lines along with a few other lines of information.
> plot(1:nv,wspec.spec(gout,:),'b',1:nv,wspec.spec(~gout,:),':k',1:nv,cent,'g',1:nv,low,'r',1:nv,up,'r');
plots all the spectra identified by logical index "gout" as blue lines ('b') and all other spectra as dotted black lines (':k') along with a green line for "cent" (1 x nv), and red lines for "low" and "up" (both 1 x nv). This works ok to produce the plot. Now I want to add a legend with 4 entries ("outliers","normal","central value" and "thresholds") as
> legend('outliers','normal','central value','thresholds')
Of course this doesn't work -- it will just put the labels on the first 5 blue lines of "wspec.spec(gout,:)" , assuming that nnz(gout)>5. What is needed is to "group" all the blue lines under a single legend entry. But you cannot just:
> p(1) = plot(1:nv,wspec.spec(~gout,:),'b','DisplayName','outliers');
since we get an error "Subscripted assignment dimension mismatch." as it wants to apply 'DisplayName' to all nnz(gout) blue lines. It would be nice if the 'DisplayName' property had this flexibility to automatically expand 'outliers' to apply to all the lines...sigh.
So, what is the best way to accomplish what I want? I know I could plot just the first line of each multiline array, generate the legend, and then plot the remaining lines of each array:
> p(1) = plot(1:nv,wspec.spec(find(gout,1,'first'),:),'b','DisplayName','outliers');
> hold on;
> p(2) = plot(1:nv,wspec.spec(find(~gout,1,'first'),:),':k','DisplayName','normal');
> p(3) = plot(1:nv,cent,'g','DisplayName','Central Value');
> p(4) = plot(1:nv,low,'r','DisplayName','Threshold');
> p(5) = plot(1:nv,up,'r');
> p(6) = plot(1:nv,wspec.spec(~gout,:),':k'); %results in duplicate plotting of first line
> p(7) = plot(1:nv,wspec.spec(gout,:),'b'); %results in duplicate plotting of first line
> hold off;
> legend(p(1:4));
but this seems un-tidy and has other problems with layering lines on top of other lines depending on the order in the plot commands. With all the changes in the graphics engine structure, I am wondering if there isn't already a way to associate legend entries to a "container" of plot lines.
3 Comments
Rik
on 27 Jan 2018
"With all the changes in the graphics engine structure" Are you referring to HG1 vs HG2?
Anyway, the dimension mismatch is due to trying to save multiple handles to one spot in an array. You can either save to a temporary array and save only 1 handle to the array, or use a cell array. I generally use the second option. This doesn't really group all lines together, but at least it works for a legend.
Dave
on 27 Jan 2018
Dave
on 27 Jan 2018
Accepted Answer
More Answers (0)
Categories
Find more on Legend in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!