以对数刻度绘制(Matlab) [英] Plotting in logarithm scale (Matlab)
问题描述
我有一组数据,应该以对数标度标绘. x值始终为正,而y值为正和负.因此,loglog
命令将只忽略负y值.但我想绘制它们:我想以对数标度表示y轴,但要同时具有负值和正值.
I have a set of data, which should be plotted in a loglog scale. x-values are always positive, but y-values are positive and negative. So, loglog
command will just omit negative y-values. But I want to plot them: I want to have y-axis in logarithmic scale, but with negative values, as well as positive values.
基本上,如果我们有一组数据(x,y)
,
我想绘制:(log(x),log(y))
,如果y>0
,和(log(x),-log(-y))
,如果y<0
.我尝试使用这些公式,然后使用简单的plot
函数,但是使用这种方法时,轴不在对数范围内.再一次,我希望两个轴都为对数刻度.
Basically, if we have set of data (x,y)
,
I want to plot: (log(x),log(y))
, if y>0
, and (log(x),-log(-y))
, if y<0
. I tried to to use these formula, and then simple plot
function, but with this approach axes are not in logarithmic scale. Once again, I want both axes to be in logarithmic scale.
谢谢
为明确起见,我希望结果与plot(x,y)
完全相同,但y轴和x轴均以对数标度
to clarify, I want the result to be exactly as plot(x,y)
, but both y-axis and x-axis to be in logarithmic scale
推荐答案
有一些用于绘制对数图的函数:沿y,沿x或两者皆有:
semilogy
, loglog
.您需要loglog
一个:
There's a few functions for plotting logarithmic plots: along y, along x or both:
semilogy
, semilogx
, loglog
. You'll need the loglog
one:
ypos = y(y>0); % Gets positive values
xpos = x(y>0); % Get corresponding x values
yneg = y(y<0); % Gets negative values
xneg = x(y<0); % Get corresponding x values
figure;
loglog(xpos,ypos)
hold on
loglog(xneg,-yneg, 'r')
这基本上在同一图中创建了两个图,一个具有正y
值,另一个具有负y
值.简而言之:
This basically creates two plots in the same figure, one with the positive and one with the negative y
values. In short:
figure;
loglog(x(y>0),y(y>0))
hold on
loglog(x(y<0),-y(y<0),'r')
由于按定义对数严格为正,因此您不能创建负标度.您可以做的就是通过设置来赋予您幻觉
Since the logarithm is by definition strict positive, you cannot create a negative scale. What you can do is give the illusion you have one by setting
set(gca, 'xdir','reverse')
这确实意味着您需要两个单独的图,以防止x轴也从正数向后移动.
This does mean you'd need two separate plots though, to prevent the x-axis from the positive numbers running backwards as well.
这篇关于以对数刻度绘制(Matlab)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!