如何在堆叠条形图matlab中放置随机标签

时间:2016-12-08 00:45:02

标签: matlab matlab-figure

我创建了一个水平堆叠的条形图,如下所示: -

data(1,:)=[0,55,87,96,97,98,99,100,102,125,130];
data(2,:)=[0,55,65,107,110,129,131,0,0,0,0];
data(3,:)=[0,60,104,108,128,130,0,0,0,0,0];
barh(data,'stacked')
axis ij
set(gca, 'xlim',[0,1000], 'box','off');

输出是这样的: enter image description here

我的问题是,我想在每个方框内随意放置标签,如下所示: -

enter image description here

标签可以是任何数字或任何字母,不一定是1。

1 个答案:

答案 0 :(得分:1)

这是一个选项:

y = repelem(1:size(data,1),size(data,2)-1);
x = (cumsum(data(:,2:end),2)-data(:,2:end)./2).';
labels = data(:,2:end).'>0;
text(x(labels),y(labels),num2str((1:nnz(labels)).'))

这将打印每个框中增加1的数字:

enter image description here

如果您想要随机输入数字,请将(1:nnz(labels)).'替换为rand(nnz(labels),1)。如果要放置字符或混合内容,请使用带有一个单元格的单元格作为标签。

相关问题