首页 文章

使用条形Matlab时右侧的空白区域

提问于
浏览
2

我正在使用以下代码来保存数字:

fig1=figure('visible','off');
b = bar(bar_res);
x={'a' ;'b' ;'c'; 'd' ;'e'; 'f' ;'g'; 'h';...
     'i'; 'j' ;'k'; 'l'; 'm'; 'n' ;'o'; 'p' ;'q' ;'r'; 's';...
     't';'u'};
set(gca,'XTickLabel',x,'XTick',1:21);
rotateXLabels( gca, 90 );
with=char('Res with dash');
without=char('Res without dash');
legend('Reference',with,without,'Location','northwest');
set(gca,'FontSize',16);
y=ylabel('Number of trials','rot',90,'FontSize',18);
set(y,'Units','Normalized','Position',[-0.15, 0.5, 0]);
savefig('a.fig');
saveas(gca, 'a.png');

但我不知道为什么右边有额外的空白区域,如下图所示:

enter image description here

2 回答

  • 0

    只需使用 xlim 设置 x-limits

    set(gca,'XLim',[0 numel(x)+1]);
    

    例:

    fig1 = figure('visible','on');
    b = bar(randi(10,21,1).');
    x = {'a' ;'b' ;'c'; 'd' ;'e'; 'f' ;'g'; 'h';...
         'i'; 'j' ;'k'; 'l'; 'm'; 'n' ;'o'; 'p' ;'q' ;'r'; 's';...
         't';'u'};
    set(gca,'XTick',1:21);
    set(gca,'XTickLabel',x);
    set(gca,'XLim',[0 numel(x)+1]);
    % rotateXLabels( gca, 90 );
    with = char('Res with dash');
    without = char('Res without dash');
    legend('Reference',with,without,'Location','northwest');
    set(gca,'FontSize',16);
    y = ylabel('Number of trials','rot',90,'FontSize',18);
    

    enter image description here

    顺便说一句,如果你有 Matlab R2014b 或更高,你不再需要 rotateXLabels . 只需使用:

    ax = gca;
    ax.XTickLabelRotation = 90;
    
  • 4

    一个简单的解决方案可能是你打电话给 axis tight . 但是它可能不是您想要的结果,因为它确实删除了绘图边界处的小空格 .

相关问题