分类直方图标签

Categorical histogram labels

我创建了一个分类直方图:

h = histogram( categorical( emotions{startIndex:endIndex,:} ) )

h.Categories
ans =
  1×5 cell array
    {'ANGER'}    {'CONTEMPT'}    {'DISGUST'}    {'JOY'}    {'SADNESS'}

>> h.Values
ans =
   164    26    18   191     1

但是似乎没有办法在直方图条上显示标签(即h.Values)。 在 this post 之后,我尝试了这个:

text(h.Values, h.Categories, num2str(h.Values'), 'HorizontalAlignment', 'Center', 'VerticalAlignment', 'Bottom' );

但我刚得到:

Error using text First two or three arguments must be numeric doubles.

但问题是我的 x 值永远不会是数字,它们是绝对的。如何解决?


对于一个可重现的例子,这样做:

emotions = { 'JOY','JOY','JOY','JOY','JOY','JOY','JOY','JOY','JOY','JOY','ANGER','ANGER','CONTEMPT','CONTEMPT','CONTEMPT','JOY','ANGER','ANGER','ANGER','ANGER'}
emotions = emotions.';
t = cell2table(emotions)
histogram(categorical(emotions))

您已经互换了 text 的前两个输入参数,并且 h.Categories 不是它需要的双精度数字(正如错误消息提示您的那样)。
所以解决方案是:

emotions = {'JOY','JOY','JOY','JOY','JOY','JOY','JOY','JOY','JOY', ...
     'JOY','ANGER','ANGER','CONTEMPT','CONTEMPT','CONTEMPT','JOY', ...
     'ANGER','ANGER','ANGER','ANGER'};
% emotions = emotions.';    %No need of this line
% t = cell2table(emotions); %No need of this line
h = histogram(categorical(emotions));

text(1:numel(h.Values), h.Values, num2str(h.Values.'), ...
    'HorizontalAlignment', 'Center', 'VerticalAlignment', 'Bottom');

结果: