Custom logarithmic axis scaling in matplotlib

前端 未结 1 1751
日久生厌
日久生厌 2020-12-02 02:21

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\'

相关标签:
1条回答
  • 2020-12-02 02:53

    The scale consists of two Transform classes, each of which needs to provide a transform_non_affine method. 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.

    Those methods need to handle numpy arrays, so they should use the respective numpy functions instead of those from the math package.

    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
    

    0 讨论(0)
提交回复
热议问题