如何生成指数缩放的轴? [英] How to produce an exponentially scaled axis?

查看:40
本文介绍了如何生成指数缩放的轴?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下代码:

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)
plt.show()

这会产生以下情节: 我的问题是,我该如何修改它,以便具有与输入完全相同的数据的图显示为一条直线?这基本上需要适当地缩放 x 轴,但我无法弄清楚如何做到这一点.这样做的原因是我显示的函数在开始时变化很小,但在有效间隔结束时开始波动更大,所以我希望在结束时有更高的水平分辨率.如果有人可以为我的方法提出替代解决方案,请随时这样做!

This produces the following plot: My question is, how can I modify this, so that the plot, with the exact same data as input, appears as a straight line? This basically requires scaling the x-axis appropriately, but I cannot figure how to do this. The reason for doing this is that I am displaying a function that changes very little at the beginning, but starts to fluctuate more towards the end of the valid interval, so I want to have a higher horizontal resolution towards the end. If anyone can propose an alternative solution to my approach, feel free to do so!

推荐答案

此处为如何 完成.一个很好的示例.您只需继承 ScaleBase 类.

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

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 10**(a/10)

        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 log2(a)*(10/log2(10))

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


mscale.register_scale(CustomScale)

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

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

这篇关于如何生成指数缩放的轴?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆