在Matlab loglog图中,轴相等

Yos*_*oun 15 matlab plot logarithm

在Matlab中命令'轴相等':

设置纵横比,使得x轴,y轴和z轴上相等的刻度线增量大小相等.这使得SPHERE(25)看起来像一个球体,而不是椭圆体

但是,使用loglog绘图功能时,这"无法正常"工作.我想要发生的是我获得宽高比,以便给定因子占据相同的视觉距离.实际发生的是

>> loglog(2.^[1:20]*1e10,(2.^[1:20]).^2)
>> axis equal
Run Code Online (Sandbox Code Playgroud)

结果是

替代文字

而不是

替代文字

因此可以很容易地观察到斜率2(从平方),并且不会有所有额外的空白区域.

我的问题是:

是否有一个Matlab命令为我这样做?或者,有人解决过这个问题吗?

gno*_*ice 10

一种解决方案是让您自己修改轴限制'DataAspectRatio'属性,以便一个轴上的十年等于十年.以下是您可以为您的示例做到的方法:

loglog(2.^[1:20]*1e10,(2.^[1:20]).^2);   %# Plot your sample data
xLimits = [1e10 1e16];                   %# Limits for the x axis
yLimits = [1 1e12];                      %# Limits for the y axis
logScale = diff(yLimits)/diff(xLimits);  %# Scale between the x and y ranges
powerScale = diff(log10(yLimits))/...    %# Scale between the x and y powers
             diff(log10(xLimits));
set(gca,'Xlim',xLimits,'YLim',yLimits,...              %# Set the limits and the
        'DataAspectRatio',[1 logScale/powerScale 1]);  %#   data aspect ratio
set(gca,'XTick',[1e10 1e12 1e14 1e16]);  %# Change the x axis tick marks
Run Code Online (Sandbox Code Playgroud)

这是由此产生的情节:

替代文字

请注意,y轴上10 0和10 2刻度线之间的间距跨越与x轴上10 10和10 12刻度线之间的空间相同的像素数,从而使一个轴上的十进制等于a十年另一个.

如果您不想更改轴限制,而是想使用MATLAB选择的默认限制,则只需从轴获取限制即可执行计算:

xLimits = get(hAxes,'XLim');
yLimits = get(hAxes,'YLim');
Run Code Online (Sandbox Code Playgroud)

然而,为了禁止MATLAB的自动调整轴的行为,你仍然必须要么设置轴限制为相同的值设置限制模式的属性,以'manual'在更新'DataAspectRatio'属性:

set(gca,'Xlim',xLimits,'YLim',yLimits,...
        'DataAspectRatio',[1 logScale/powerScale 1]);
%# OR...
set(gca,'XLimMode','manual','YLimMode','manual',...
        'DataAspectRatio',[1 logScale/powerScale 1]);
Run Code Online (Sandbox Code Playgroud)

如果所有这些看起来像很多工作,你可以通过把它全部放入一个函数来简化事情.我实际上将根据本答案中的代码decades_equalMathWorks文件交换提交一个函数.目前,这里有一个可以使用的修剪版本(即没有错误检查或帮助):

function decades_equal(hAxes,xLimits,yLimits)

  if (nargin < 2) || isempty(xLimits)
    xLimits = get(hAxes,'XLim');
  end
  if (nargin < 3) || isempty(yLimits)
    yLimits = get(hAxes,'YLim');
  end

  logScale = diff(yLimits)/diff(xLimits);
  powerScale = diff(log10(yLimits))/diff(log10(xLimits));

  set(hAxes,'Xlim',xLimits,...
            'YLim',yLimits,...
            'DataAspectRatio',[1 logScale/powerScale 1]);

end
Run Code Online (Sandbox Code Playgroud)

您可以按如下方式调用该函数:

loglog(2.^[1:20]*1e10,(2.^[1:20]).^2);   %# Plot your sample data
decades_equal(gca);                      %# Make the decades equal sizes
Run Code Online (Sandbox Code Playgroud)


这个怎么运作...

您可能想知道我选择上述缩放因子背后的逻辑是什么.当试图使每个轴的显示大小相等时,我们必须考虑轴范围内数十年的数量和大小.在上面的代码中,我基本上计算每个轴的平均十进制大小,然后使用平均十进制大小的比率来相应地缩放轴.例如,diff(yLimits)给出y轴的总大小,并diff(log10(yLimits))给出y轴上显示的十进制数(即10的幂).

如果我重新排序上述代码中的操作,这可能更容易看到:

yDecade = diff(yLimits)/diff(log10(yLimits));  %# Average y decade size
xDecade = diff(xLimits)/diff(log10(xLimits));  %# Average x decade size
set(gca,'XLim',xLimits,'YLim',yLimits,...
        'DataAspectRatio',[1 yDecade/xDecade 1]);
Run Code Online (Sandbox Code Playgroud)

这将提供与以前相同的缩放结果.