Matplotlib中自定义对数坐标轴缩放

6

我正在尝试使用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()

请注意,matplotlib中的常规对数刻度使用以10为底的对数。而math.log()定义了自然对数(以e为底)。您可能需要更清楚地表明您想要使用哪种对数。 - ImportanceOfBeingErnest
哎呀,你说的对。我是指 math.log10! - Arnold
你好,看起来你根本没有使用 thresh?如果删除这个在你的 MWE 中不会有影响吗? - Reinderien
2个回答

6

该比例尺由两个Transform类组成,每个类都需要提供一个transform_non_affine方法。其中一个类需要将数据转换为显示坐标,即log(a+1),另一个是反向的,需要将显示坐标转换为数据坐标,在这种情况下应该是exp(a)-1

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

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

enter image description here


太好了!非常感谢您提供如此清晰的解释! - Arnold

1

无需自己定义类,即使来自@ImportanceOfBeingErnest的答案也可以使用。

你可以使用FunctionScaleFunctionScaleLog中的任一项在一行代码中实现此操作。以FunctionScaleLog为例:

plt.gca().set_xscale("functionlog", functions=[lambda x: x + 1, lambda x: x - 1])

并附上您的完整代码:

import matplotlib.pyplot as plt
import numpy as np

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)

# Below is my code
plt.gca().set_xscale("functionlog", functions=[lambda x: x + 1, lambda x: x - 1])
plt.gca().set_xticks(np.arange(0, 6))
plt.gca().set_xticklabels(np.arange(0, 6))

结果是:

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接