Matplotlib的“symlog”选项:如何防止曲线“卷土重来”?

2024-09-28 21:56:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我在绘制数据范围,以x为单位,从-1000到1000。但我只对x=-1和0之间的值感兴趣。在

我还想用x对数标度来绘图。但是由于x值是负的,我不能使用xscale(“log”)。我可以使用xscale(“symlog”),这是我想要的行为。在

不幸的是,“symlog”似乎坏了。我不能使用值小于2(默认值?)的linthreshx参数。但由于我对从-1到0的x值感兴趣,我必须使用该参数并将其设置为类似于1e-5甚至更小的值。在

如果我将linthreshx设置为小于1的值,则绘图中断。下面是一个来自What is the difference between 'log' and 'symlog'?的简单示例:

import numpy
from matplotlib import pyplot

# Enable interactive mode
pyplot.ion()

# Draw the grid lines
pyplot.grid(True)

# Numbers from -50 to 50, with 0.1 as step
xdomain = numpy.arange(-50,50, 0.1)

# Plots a simple linear function 'f(x) = x'
pyplot.plot(xdomain, xdomain)
# Plots 'sin(x)'
pyplot.plot(xdomain, numpy.sin(xdomain))

pyplot.axis([-60,60,-1.5,1.5])

pyplot.xscale('symlog', linthreshx=0.1)

你会明白我说的曲线“回来”是什么意思。。。以下是生成的图像:result

问题似乎是,在x轴上,0实际上是10^0=1,而不是0。放置小于1的值将使直线返回,轴值错误(当鼠标悬停并获取x值时)。在

我可能没有使用正确的工具,但如何实现我想要的?我希望x轴看起来像:-10^2-10^1-10^0-10^-1-10^-2-10^-3。。。[达到我定义的最小指数]。。。10^-3 10^-2 10^-1 10^0 10^1 10^2

谢谢你


Tags: thefromimportnumpylog绘图参数感兴趣