[英]Legend in a bar plot in Matlab
How can I plot a legend in a bar plot in Matlab? 如何在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 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.
我尝试过在SO和网络上找到的解决方案,但我无法解决这个问题。
Instead of legend, you can solve it using the tick labels for example: 您可以使用刻度标签解决它,而不是图例,例如:
set(gca,'xticklabel', l)
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. 如果要使用
legend
,则需要有矩阵数据,因此条形图将在每个条目中显示几个条形图。 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);
Or, you can use different bar()
calls in this way: 或者,您可以通过以下方式使用不同的
bar()
调用:
h = bar(diag(Y));
But then you'll get a displacement per each bar: 但是你会得到每个酒吧的位移:
So, the only way really to do that using legend
is to plot each bar
seperatly, similar to what is discussed here . 因此,使用
legend
真正做到这一点的唯一方法是分别绘制每个bar
,类似于此处讨论的内容。
Further to bla's answer, you can use 除了bla的答案,你可以使用
h = bar(diag(Y),'stacked');
if you want to avoid the displacement. 如果你想避免位移。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.