White space on the right when using bar Matlab

前端 未结 2 490
南方客
南方客 2021-01-21 22:02

I\'m using the below code to save a figure:

fig1=figure(\'visible\',\'off\');
b = bar(bar_res);
x={\'a\' ;\'b\' ;\'c\'; \'d\' ;\'e\'; \'f\' ;\'g\'; \'h\';...
            


        
2条回答
  •  甜味超标
    2021-01-21 22:08

    Just set the x-limits with xlim:

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

    Example:

    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

    By the way, if you have Matlab R2014b or higher you don't need rotateXLabels anymore. Just use:

    ax = gca;
    ax.XTickLabelRotation = 90;
    

提交回复
热议问题