简体   繁体   中英

How to display only selected legend entries for bar3 plot in MATLAB?

Consider the following code:

A=0:0.1:4;
for i=1:50,
    B(:,i) = sin(A+i*0.01); % each column of B contains "shifted" sin
end
bar3(B); % plot such as each "shifted" sin will have different color
rr=1:size(B,1); % numbers to label different "shifted" sin in legend
l=strtrim(cellstr(num2str(rr'))') % converting numerical labels to strings accepted by "label"
legend(l);

在此处输入图片说明

How to display legend entries only for selected profiles eg 1st, 25th and last?

Question is similar to: How to show legend for only a specific subset of curves in the plotting? But I do not know how to get the figure handles for bar3 as suggested in the answer. Alternatively: does more elegant solution exist?

You get the handles with:

h = bar3(B); % plot such as each "shifted" sin will have different color

Then you can display the legend for selected profiles only:

legend(h([1 25 end]), l{[1 25 end]})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM