Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python matplotlib中的自定义对数轴缩放_Python_Matplotlib_Axes_Logarithm - Fatal编程技术网

Python matplotlib中的自定义对数轴缩放

Python matplotlib中的自定义对数轴缩放,python,matplotlib,axes,logarithm,Python,Matplotlib,Axes,Logarithm,我尝试用math.log(1+x)来缩放绘图的x轴,而不是通常的“log”缩放选项,我已经查看了一些自定义缩放示例,但我的无法正常工作!这是我的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

我尝试用math.log(1+x)来缩放绘图的x轴,而不是通常的“log”缩放选项,我已经查看了一些自定义缩放示例,但我的无法正常工作!这是我的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 math.log(1+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 math.log(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 = [0,0.1,0.3,0.9,1,2,5]
thick = [20,40,20,60,37,32,21]

fig = plt.figure(figsize=(8,5))
ax1 = fig.add_subplot(111)
ax1.plot(z, thick, marker='o', 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()

scale由两个变换类组成,每个变换类都需要提供一个
变换\u非仿射
方法。一个类需要从数据转换到显示坐标,这将是
log(a+1)
,另一个类是相反的,需要从显示坐标转换到数据坐标,在这种情况下是
exp(a)-1

这些方法需要处理numpy数组,因此它们应该使用相应的numpy函数,而不是数学包中的函数

class CustomTransform(mtransforms.Transform):
    ....

    def transform_non_affine(self, a):
        return np.log(1+a)

class InvertedCustomTransform(mtransforms.Transform):
    ....

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

请注意,matplotlib中常用的对数刻度使用以10为底的对数
math.log()
定义自然对数(以e为底)。你可能想弄清楚,你想用哪个对数。哎呀,你说得对。我是说数学!您似乎根本不使用
thresh
?这个能从你的MWE上掉下来而不受影响吗?太好了!非常感谢你的解释,非常清晰!