与零以外的其他点对数的比例

时间:2018-02-07 14:32:48

标签: python matplotlib scale logarithm

我在区间[0,1]上定义了一个函数(所有数字的范围从0到1,不包括1),它在1附近有一些不稳定的行为,例如函数f = lambda x : 1 / (1-x)。我可以使用以下方式绘制它:

import matplotlib.pyplot as plt
x_range = np.linspace(0,1,num=100)
plt.plot(x_range, f(x_range))
plt.show()

如果渐近行为为0,我们只需使用对数x刻度,但现在渐近线为1,因此我们希望对数刻度为1,我们的刻度可以是:1 - 10 ^ 0 ,1 - 10 ^ 1,1,10 ^ 2,1,10 ^ 3,....

编辑:我刚刚发现了this link,所以我想我可以制作自己的尺度,这是我目前要做的。

使用评论中提供的链接,我得到了以下MWE:

import matplotlib.pyplot as plt
import numpy as np
import math
from matplotlib.ticker import FormatStrFormatter
from matplotlib import scale as mscale
from matplotlib import transforms as mtransforms

class CustomScale(mscale.ScaleBase):
    name = 'custom'

    def __init__(self, axis, **kwargs):
        mscale.ScaleBase.__init__(self)
        self.thresh = None #thresh

    def get_transform(self):
        return self.CustomTransform(self.thresh)

    def set_default_locators_and_formatters(self, axis):
        pass

    class CustomTransform(mtransforms.Transform):
        input_dims = 1
        output_dims = 1
        is_separable = True

        def __init__(self, thresh):
            mtransforms.Transform.__init__(self)
            self.thresh = thresh

        def transform_non_affine(self, a):
            return 1 - 10 **(-a)

        def inverted(self):
            return CustomScale.InvertedCustomTransform(self.thresh)

    class InvertedCustomTransform(mtransforms.Transform):
        input_dims = 1
        output_dims = 1
        is_separable = True

        def __init__(self, thresh):
            mtransforms.Transform.__init__(self)
            self.thresh = thresh

        def transform_non_affine(self, a):
            return -np.log10(1-a)

        def inverted(self):
            return CustomScale.CustomTransform(self.thresh)

# Now that the Scale class has been defined, it must be registered so
# that ``matplotlib`` can find it.
mscale.register_scale(CustomScale)

z = np.linspace(0,0.9999,num = 100)
thick = 1/(1-z)

fig = plt.figure(figsize=(8,5))
ax1 = fig.add_subplot(111)
ax1.plot(z, thick, linewidth=2, c='k')

plt.xlabel(r'$\rm{redshift}$', size=16)
plt.ylabel(r'$\rm{thickness\ (kpc)}$', size=16)
plt.gca().set_xscale('custom')
plt.show()

这并不完全符合预期,所以我猜我设置转换时出错了。

0 个答案:

没有答案