简体   繁体   English

matplotlib 中的自定义对数轴缩放

[英]Custom logarithmic axis scaling in matplotlib

I'm trying to scale the x axis of a plot with math.log(1+x) instead of the usual 'log' scale option, and I've looked over some of the custom scaling examples but I can't get mine to work: Here's my MWE:我正在尝试使用 math.log(1+x) 而不是通常的“log”缩放选项来缩放 plot 的 x 轴,并且我查看了一些自定义缩放示例,但我无法获取我的工作:这是我的 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()

The scale consists of two Transform classes, each of which needs to provide a transform_non_affine method. scale由两个Transform类组成,每个类都需要提供一个transform_non_affine方法。 One class needs to transform from data to display coordinates, which would be log(a+1) , the other is the inverse and needs to transform from display to data coordinates, which would in this case be exp(a)-1 .一类需要从数据转换为显示坐标,即log(a+1) ,另一类是相反的,需要从显示转换为数据坐标,在本例中为exp(a)-1

Those methods need to handle numpy arrays, so they should use the respective numpy functions instead of those from the math package.这些方法需要处理 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

在此处输入图片说明

There's no need to define classes yourself even the answer from @ImportanceOfBeingErnest does work.即使来自@ImportanceOfBeingErnest 的答案确实有效,也无需自己定义类。

You can use either FunctionScale or FunctionScaleLog to do this in one line .您可以使用FunctionScaleFunctionScaleLog一行中执行此操作。 Take the FunctionScaleLog as example:FunctionScaleLog为例:

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

And with your full code:并使用您的完整代码:

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))

And the result is:结果是:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM