White space on the right when using bar Matlab

核能气质少年 提交于 2019-12-02 01:42:36

问题


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

But I don't know why there is extra white space to the right as shown in the below figure:


回答1:


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

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

ax = gca;
ax.XTickLabelRotation = 90;



回答2:


A simple solution may be if you call axis tight. However it may not be the result you want because it does remove the small spaces at the borders of the plot.



来源:https://stackoverflow.com/questions/29775842/white-space-on-the-right-when-using-bar-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!