【问题标题】:Categorical histogram labels分类直方图标签
【发布时间】:2018-04-22 20:11:24
【问题描述】:

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

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' );

但我只是得到:

错误使用文本前两个或三个参数必须是数字双精度。

但问题是我的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))

【问题讨论】:

    标签: matlab histogram matlab-figure categorical-data


    【解决方案1】:

    您已经交换了texth.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');
    

    结果:

    【讨论】:

    • 非常感谢您抽出宝贵时间查看 Sardar,它解决了我的问题。
    猜你喜欢
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    • 2015-12-17
    • 2013-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-04
    相关资源
    最近更新 更多