matlab - Matlab 条形图中的图例

标签 matlab plot legend

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

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 上找到的解决方案和网络,但我无法解决这个问题。

最佳答案

代替图例,您可以使用刻度标签解决它,例如:

set(gca,'xticklabel', l) 

enter image description here

这将标记每个栏。如果你想使用 legend,你需要有一个矩阵数据,所以条形图将显示每个条目的几个条。例如

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

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

h = bar(diag(Y));

但是你会得到每个小节的位移:

enter image description here

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

关于matlab - Matlab 条形图中的图例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14697525/

相关文章:

python - 如何将图形图例重新定位到子图形中

legend - 使用 Geotools JMapFrame 显示图例

arrays - Matlab:将int数组转换为字符串数组?

c++ - Matlab编译成C++问题: fatal error C1083: Cannot open include file: 'windows.h'

具有按元素运算的 MATLAB 符号输出

r - 如何在R中绘制多条线

r - R中谷歌地图背景上的重叠图像图

plot - Julia 与原子的现场情节

legend - AmChart 图例一行

python - 在 Matlab 排序中从 numpy 数组顺序获取数据