Matlab中的Axis Label旁边没有间隙? [英] No Gap Next to Axis Label in Matlab?

查看:103
本文介绍了Matlab中的Axis Label旁边没有间隙?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Matlab 2015b或Matlab 2016a.

Matlab 2015b or Matlab 2016a.

我想让网格线穿过子图在图形之间的间距,以便更好地水平评估两张图片. 但是,我在右下角的两个图形之间有一个小缝隙,因为这使图形没有对齐

I would like to have grid lines going across Subplot's spacing between the figures in order to evaluate better two pictures horizontally. However, I have a small gap between the two figures at the lower right-hand-side corner because which is misaligning the figures

出现间隙的原因是右下角的10^4. 我也希望水平线跨越两个图形之间的间距,但是在解决间隙问题之前我做不到.

where the gap is because of the 10^4 at lower right-hand-side corner. I would also like to have horizontal lines going across the spacing between the two figures, but I cannot do it before the gap problem is solved.

按照答案此处中所述的关于带有色标的螺纹 Tight子图进行相对对齐的代码和Matlab中子图的第三个参数?

Code where the relative alignment is done as described in the answer here about the thread Tight subplot with colorbars and subplot's 3rd parameter in Matlab?

data=randi(513,513);
D=mat2gray(pdist(data, 'correlation'));

% Set normalized outer position (x,y,width,height)
ax1=axes('OuterPosition', [0 0.5 0.5 0.5]);
plot(D, 'Parent', ax1);
xlim([0 size(D,2)]);
set(cbar1, 'Visible', 'off')
title('Signal');

ax2=axes('OuterPosition', [0.51 0.5 0.5 0.5]);
plot(D, 'Parent', ax2);
set(ax2, 'XLim', [0, size(D,1)])
axis(ax2, 'square');
title('Corr pdist');

Suever的 answer

的输出

我尝试将sprintf('%.2g', x)中的两(2)更改为更大和更小

Output of Suever's answer

I tried unsuccessfully change two (2) in sprintf('%.2g', x) bigger and smaller

ax2 = axes('OuterPosition', [0.51 0.5 0.5 0.5]);
plot(D, 'Parent', ax2);
set(ax2, 'XLim', [0, size(D,1)])
axis(ax2, 'square');
title('Corr pdist');
cbar2 = colorbar(); % ax2 not needed here in brackets
set(ax2, 'XLim', [0 size(D,2)]);
set(cbar2, 'Visible', 'off')
grid minor;
% https://stackoverflow.com/a/35776785/54964
xticks = get(ax2, 'xtick');
labels = arrayfun(@(x)sprintf('%.2g', x), xticks, 'uniform', 0);
set(ax2, 'xticklabels', labels);

它给出了

那些刻度不是XMinorTicks而是刻度(在图中错误地标记). 它们在x轴上的某些点为零.当x轴变大时,MATLAB自动添加新的xtick标记,但没有完整的标签. 我认为最好在这里再有一个符号而不是零.对于xticks的不完整标签,您怎么能得到零以外的其他标记?

where those ticks are not XMinorTicks but simply ticks (wrongly marked in the picture). They are zero points at some points in the x-axis. When x-axis gets larger, MATLAB automatically adds new xtick marks but without complete labels. I think it would be better to have another symbol than zero there. How can you have some other mark than zero for incomplete labels of xticks?

如何将第二个图形中最后一个数字旁边的10 ^ 4对齐?

How can you align the 10^4 next to the last number in the second figure?

推荐答案

我将获得当前的xtick位置,将其转​​换为字符串,然后设置轴的xticklabels属性.

I would get the current xtick locations, convert those to strings, and then set the xticklabels property of the axes.

xticks = get(ax2, 'xtick');
labels = arrayfun(@(x)sprintf('%.2g', x), xticks, 'uniform', 0);
set(ax2, 'xtick', xticks, 'xticklabels', labels);

如果您希望随着图形的大小变化而动态地计算它们(并重新计算xticks),则可以将此代码链接到图形的SizeChangedFcn.

If you want them to dynamically be computed as the figure changes size (and the xticks get recomputed) you can link this code to the SizeChangedFcn of the figure.

func = @(varargin)set(ax2,'xticklabels',arrayfun(@(x)sprintf('%.2g',x),get(ax2, 'xtick'),'uni',0));
set(gcf, 'SizeChangedFcn', func)

这篇关于Matlab中的Axis Label旁边没有间隙?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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