Matlab条形图中的图例 [英] Legend in a bar plot in Matlab

查看:718
本文介绍了Matlab条形图中的图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Matlab的条形图中绘制图例?这是代码:

How can I plot a legend in a bar plot 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);

这会出现错误:警告:忽略多余的图例条目.我尝试了在 SO 上找到的解决方案,并网络,但我无法解决此问题.

This give an error: Warning: Ignoring extra legend entries. I tried solutions which I found on the SO and web, but I couldn't resolve this.

推荐答案

例如,您可以使用对勾标签来解决此问题,而不是使用图例:

Instead of legend, you can solve it using the tick labels for example:

set(gca,'xticklabel', l) 

这将标记每个条.如果要使用legend,则需要具有矩阵数据,因此条形图将在每个条目中显示多个条形.例如

This will label each bar. If you want to use legend you need to have a matrix data, so the bar plot will show several bars per entry. For example

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);

或者,您可以通过以下方式使用不同的bar()调用:

Or, you can use different bar() calls in this way:

h = bar(diag(Y));

但随后每根钢筋都会产生位移:

But then you'll get a displacement per each bar:

因此,使用legend真正做到这一点的唯一方法是分别绘制每个bar,类似于所讨论的此处.

So, the only way really to do that using legend is to plot each bar seperatly, similar to what is discussed here.

这篇关于Matlab条形图中的图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆