2017-04-18 1 views
2

일반적인 '로그'배율 옵션 대신 math.log (1 + x)를 사용하여 플롯의 x 축 배율을 조정하려고합니다. 일부 사용자 지정 스케일링 예제지만 내 작품을 얻을 수 없어! 여기 내 MWE는 다음과 같습니다matplotlib의 사용자 지정 로그 축 배율

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() 
+0

노트'(기지국의 E) 자연 로그를 정의한다. 그것을 사용하고자하는 로그를 명확하게하고 싶을 수 있습니다. – ImportanceOfBeingErnest

+0

죄송합니다. 나는 수학을 말했어 .log10! – Arnold

답변

2

규모가 transform_non_affine 방법을 제공 할 필요가 각각 두 개의 변환 클래스로 구성되어 있습니다. 하나의 클래스는 데이터에서 log(a+1)이 될 좌표를 표시하기 위해 변환해야하며, 다른 하나는 역함수이며 디스플레이에서 데이터 좌표로 변환해야합니다.이 경우는 exp(a)-1입니다.

이러한 메서드는 numpy 배열을 처리해야하므로 수학 패키지의 numpy 함수 대신 numpy 함수를 사용해야합니다.

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 
하기 matplotlib에서 통상의 로그 스케일 math.log베이스 (10) '()의 로그를 사용

enter image description here

+0

완벽한! 매우 명확하고 명확한 설명을 해주셔서 너무 감사드립니다! – Arnold