2013-12-12 131 views
1

我需要在3个酒吧在matlab中做一个条形图。三个小节的值存储在x中。现在我想创建一个图例:每个栏应该用一个字母标记,例如'A', 'B', 'C'。matlab酒吧情节:标签3酒吧每个只有一个值

x = rand(1,3); bar(x); legend('A','B','C'); 

没有工作。 后来我试过了这个例子:http://www.mathworks.com/matlabcentral/fileexchange/35271-matlab-plot-gallery-vertical-bar-plot/content/html/Vertical_Bar_Plot.html

但只要你减少在每个类别中的条目数为1,你会得到一个错误信息,但我认为该消息是错误= /它的工作原理与任何但只是不与1 ...

那么是否有一个简单的解决方案,以解决这个问题?

回答

0

你想要的是一个“三连串图” - 不幸的是,只要你把它做成1x3,Matlab认为它只是一个系列。我们必须欺骗它以为你有三个系列。这里是你如何做到这一点:

x = rand(1,3); 
x2 = [x; x*0]; % add a second row of all zeros 
figure 
bar(x2);   % now we have a plot with an ugly second category 
xlim([0.5 1.5]); % trick that hides second category 
legend('a','b','c'); % add the legend 
set(gca, 'XTickLabel', ''); % hide the '1' on the X axis (or make it whatever you want) 
title 'Bar plot with legend - demo' 

结果则是这样的:

enter image description here

我相信这是你问的什么了。

+0

非常感谢!这真的就是我正在寻找的东西! – flawr

+0

我确实想到,绘制单个系列并更改'XTickLabel'以反映图例(“分类轴”)会更传统(a,b,c而不是X轴上的1,2,3) - 但这不是你要求的。就像这样你会得到不同的颜色...... – Floris