使用Bar Matlab时右侧的空白 [英] White space on the right when using bar Matlab

查看:317
本文介绍了使用Bar Matlab时右侧的空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码保存图形:

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:

推荐答案

只需使用xlim设置 x限制:

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

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

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

ax = gca;
ax.XTickLabelRotation = 90;

这篇关于使用Bar Matlab时右侧的空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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