Skip to content Skip to sidebar Skip to footer

How To Produce An Exponentially Scaled Axis?

Consider the following code: from numpy import log2 import matplotlib.pyplot as plt xdata = [log2(x)*(10/log2(10)) for x in range(1,11)] ydata = range(10) plt.plot(xdata, ydata) p

Solution 1:

Here is how it is done. A good example to follow. You just subclass the ScaleBase class.

Here's your transform. It's not too complicated when you whittle out all the custom formatters and stuff. Just a little verbose.

from numpy import log2
import matplotlib.pyplot as plt

from matplotlib import scale as mscale
from matplotlib import transforms as mtransforms

classCustomScale(mscale.ScaleBase):
    name = 'custom'def__init__(self, axis, **kwargs):
        mscale.ScaleBase.__init__(self)
        self.thresh = None#threshdefget_transform(self):
        return self.CustomTransform(self.thresh)

    defset_default_locators_and_formatters(self, axis):
        passclassCustomTransform(mtransforms.Transform):
        input_dims = 1
        output_dims = 1
        is_separable = Truedef__init__(self, thresh):
            mtransforms.Transform.__init__(self)
            self.thresh = thresh

        deftransform_non_affine(self, a):
            return10**(a/10)

        definverted(self):
            return CustomScale.InvertedCustomTransform(self.thresh)

    classInvertedCustomTransform(mtransforms.Transform):
        input_dims = 1
        output_dims = 1
        is_separable = Truedef__init__(self, thresh):
            mtransforms.Transform.__init__(self)
            self.thresh = thresh

        deftransform_non_affine(self, a):
            return log2(a)*(10/log2(10))

        definverted(self):
            return CustomScale.CustomTransform(self.thresh)


mscale.register_scale(CustomScale)

xdata = [log2(x)*(10/log2(10)) for x inrange(1,11)]
ydata = range(10)
plt.plot(xdata, ydata)

plt.gca().set_xscale('custom')
plt.show()

Solution 2:

The easiest way is to use semilogy

from numpy import log2
import matplotlib.pyplot as plt

xdata = log2(range(1,11)) * (10/log2(10))
ydata = range(10)
plt.semilogy(xdata, ydata)
plt.show()

enter image description here

Post a Comment for "How To Produce An Exponentially Scaled Axis?"