![]() |
Matlab对数图中的轴相等
在Matlab中,命令“轴相等”:
[INDENT]设置纵横比,以使x,y和z轴上相等的刻度标记增量大小相等。这使SPHERE(25)看起来像球体,而不是椭圆体 [/INDENT]然而,在使用时, loglog绘图功能,这不工作“正常”。我想做的是得到一个长宽比,以便给定的[B]因素[/B]占据相同的视觉距离。实际发生的是 >> loglog(2.^[1:20]*1e10,(2.^[1:20]).^2) >> axis equal 结果是 [IMG]https://i.stack.imgur.com/dX1eX.png[/IMG] 而不是 [IMG]https://i.stack.imgur.com/4T09V.png[/IMG] 这样就可以很容易地观察到斜率2(从平方数开始),从而不会有所有额外的空白。 我的问题是: [INDENT]有Matlab命令可以帮我吗?或者,有人解决过这个问题吗? [/INDENT] [B]回答:[/B] 一种解决方案是让您自己修改[URL="http://www.mathworks.com/help/techdoc/ref/axes_props.html#XLim"]轴限制[/URL]和[URL="http://www.mathworks.com/help/techdoc/ref/axes_props.html#DataAspectRatio"]'DataAspectRatio'[/URL]属性,以使一个轴上的十年等于另一个轴上的十年。对于您的示例,可以按照以下方法进行操作: 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 这是结果图: [IMG]https://i.stack.imgur.com/nUvhU.png[/IMG] 请注意,y轴上的10 0和10 2刻度线之间的间隔与x轴上的10 10和10 12刻度线之间的间隔跨越相同数量的像素,因此使一个轴上的十进制等于a另一方面。 如果您不想更改轴限制,而是想使用MATLAB选择的默认限制,则可以简单地从轴获取限制以执行计算: xLimits = get(hAxes,'XLim'); yLimits = get(hAxes,'YLim'); 但是,为了禁用MATLAB的自动轴大小调整行为,您仍然必须在更新'DataAspectRatio'属性时将轴限制设置为相同的值, [B]或者[/B]将[URL="http://www.mathworks.com/help/techdoc/ref/axes_props.html#XLimMode"]限制模式属性[/URL]设置为'manual' : set(gca,'Xlim',xLimits,'YLim',yLimits,... 'DataAspectRatio',[1 logScale/powerScale 1]); %# OR... set(gca,'XLimMode','manual','YLimMode','manual',... 'DataAspectRatio',[1 logScale/powerScale 1]); 如果所有这些工作看起来都很繁琐,则可以通过将其全部放入函数中来简化操作。我实际上将根据此答案中的代码向[URL="http://www.mathworks.com/matlabcentral/fileexchange/"]MathWorks File Exchange[/URL]提交一个功能decades_equal的函数。目前,这里是您可以使用的精简版本(即没有错误检查或帮助): 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 您可以按以下方式调用该函数: loglog(2.^[1:20]*1e10,(2.^[1:20]).^2); %# Plot your sample data decades_equal(gca); %# Make the decades equal sizes [B]怎么运行的... [/B] 您可能想知道我如何选择上述比例因子背后的逻辑。当尝试使每个轴的十进制显示大小相等时,我们必须考虑轴范围内的十进制数和大小。在上面的代码中,我基本上是在计算每个轴的平均十进位大小,然后使用平均十进位大小的比率来相应地缩放轴。例如, 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]); 并且这将提供与以前相同的缩放结果。 [url=https://stackoverflow.com/questions/4133510]更多&回答...[/url] |
所有时间均为北京时间。现在的时间是 23:28。 |
Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.