如何将xlabel的位置居中,使其位于图形的中间?在使用LaTeX时,我希望xlabel的中心与标题的中心对齐。
发布于 2012-06-25 20:10:13
xlabel函数创建一个string图形对象,并将其设置为当前axes对象的XLabel属性。您可以在调用xlabel时定义此string对象的属性。可以通过调整Position属性来调整string对象的中心位置,该属性默认设置为0 0。
首先,您将获得当前位置(在绘制并使用xlabel之后):
vec_pos = get(get(gca, 'XLabel'), 'Position');然后更新位置(例如,使用-0.5调整x):
set(get(gca, 'XLabel'), 'Position', vec_pos + [-0.5 0 0]);这是在数据单元中完成的,默认情况下,就文档而言,x轴是这样做的。在我看来,标签"Time (s)“位于0.13秒(根据您的数字)。让我们把它向左调整0.008秒到0.122秒(一个“猜测”)。
将其强制为“数据”单位,并调整为0.008:
str_defaultUnits = get(get(gca, 'XLabel'), 'Units'); % copy this
set(get(gca, 'XLabel'), 'Units', 'data'); % change it
set(get(gca, 'XLabel'), 'Position', vec_pos + [-0.008 0 0]); % adjust position
set(get(gca, 'XLabel'), 'Units', str_defaultUnits); % set it back as it was:我想说的是,你试图实现的目标有些错误:)轴的标签不应该强制与整个图形标题对齐。:我会说:)轴的标签不应该强制与整个图形标题对齐。为什么要这样做?图形标题位于整个图形的中心,而不仅仅是绘图区域。我担心它最终会看起来很奇怪。当然是你的选择。
发布于 2012-12-20 21:51:06
你想要做什么的细节将取决于你的matlab打印设置和latex选项(例如,标题是右对齐还是居中),但这至少应该把你的xlabel放在图形的中心,而不是轴的中心。
fh=figure;
ah=axes;
plot(ah,[2.0:10],[2:10])
xlh=xlabel(ah,'my xlabel');
drawnow;
xlh_pos=get(xlh,'position');
ah_pos=get(ah,'position');
x_lim=xlim;
xlh_pos_fig=0.5;%put it in the middle
xlh_pos(1)=(xlh_pos_fig - ah_pos(1))*(x_lim(2)-x_lim(1))/ah_pos(3)+x_lim(1);
set(xlh,'position',xlh_pos);https://stackoverflow.com/questions/11187037
复制相似问题