The legend at the bar in Matlaba

How can I build a legend on a chart in Matlab? Here is the code:

Y = [1.5056 0.72983 3.4530 3.2900 1.4839 12.9 ]; n = length(Y); h = bar(Y); colormap(summer(n)); grid on l = cell(1,6); l{1}='L'; l{2}='B'; l{3}='R'; l{4}='P'; l{5}='h'; l{6}='Ri'; legend(h,l); 

This will give an error: Warning: Ignoring additional legend entries. I tried the solutions I found on /qaru.site / ... and on the Internet, but I couldn’t resolve this.

+4
source share
2 answers

Instead of a legend, you can solve this problem using label labels, for example:

 set(gca,'xticklabel', l) 

enter image description here

This will mean every bar. If you want to use legend , you need to have matrix data, so several columns per record will be displayed on the panel chart. for instance

 Y=rand(10,6) h = bar(Y); colormap(summer(n)); grid on l = cell(1,6); l{1}='L'; l{2}='B'; l{3}='R'; l{4}='P'; l{5}='h'; l{6}='Ri'; legend(h,l); 

enter image description here

Or you can use different calls to bar() as follows:

 h = bar(diag(Y)); 

But then you get an offset for each bar:

enter image description here

Thus, the only way to do this with legend is to build each bar separately, similar to what was discussed here .

+15
source

Besides bla's answer, you can use

 h = bar(diag(Y),'stacked'); 

if you want to avoid bias.

+1
source

All Articles