Matlab:条形图中缺少标签 [英] Matlab: Missing labels in bar chart

查看:190
本文介绍了Matlab:条形图中缺少标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Matlab 2012和2013,我发现在 bar 图表上设置 XTickLabel 只能使用最多15个酒吧。如果有更多栏,则缺少标签,如下所示。

With Matlab 2012 and 2013, I found that setting XTickLabel on a bar chart only works with up to 15 bars. If there are more bars, labels are missing, as shown below.

绘制15个酒吧:

N = 15;
x = 1:N;
labels = num2str(x', '%d');
bar(x);
set(gca, 'XTickLabel', labels);

绘制16个酒吧:

N = 16;
x = 1:N;
labels = num2str(x', '%d');
bar(x);
set(gca, 'XTickLabel', labels);

对于 N& 15 ,它将始终只显示10个标签。

For N > 15, it will always only display 10 labels.

有其他人体验过吗?任何解决方法?

Does anyone else experience this? Any work-arounds? I need all labels because I am plotting discrete categories and not a continuous function.

推荐答案

这是因为点击标签必须与本身匹配。在你给出的例子中,使用 N = 16; x = 1:N; code> XTick (至少在您和我的计算机上):

This happens because the tick labels have to match the ticks themselves. In the example you gave with N = 16; and x = 1:N;, MATLAB automatically makes the following XTicks (on your and my machines, at least):

>> xticks = get(gca,'xtick')
xticks =
     0     2     4     6     8    10    12    14    16    18
>> numel(xticks)
ans =
    10

不同的酒吧。因此,当你运行 set(gca,'XTickLabel',labels); labels = num2str(x','%d'); / code>(16个标签),它给出了第二个数字,你显示错误的标签和刻度线之前/之后的柱(位置0和18)。

Just 10 ticks for the 16 different bars. Thus, when you run set(gca, 'XTickLabel', labels); with labels = num2str(x', '%d'); (16 labels), it gives the second figure you showed with the wrong labels and ticks before/after the bars (at positions 0 and 18).

要为每个栏设置标记标签,还需要将标记设置为匹配:

To set a tick label for each bar, you also need to set the ticks to match:

set(gca,'XTick',x) % this alone should be enough
set(gca,'XTickLabel',labels);

然后您将得到所需的结果:

Then you will get the desired result:

无论何种原因,16似乎是MathWorks决定的奇数: XTick s不应该为每个条形图绘制,如果需要,可以由用户设置它们。

For whatever reason, 16 seems to be the magic number at which MathWorks decided XTicks should not be drawn for each bar, leaving it up to the user to set them if needed.

这篇关于Matlab:条形图中缺少标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆