如何解决使用Matplotlib自定义轴
我正在尝试自定义我的x比例,使其看起来像这样(对数从1到5,十进制从5到10,依此类推): enter image description here
这是我的第一次尝试(我仅尝试使其从1到5的对数):
import numpy as np
from matplotlib.ticker import FormatStrFormatter
from matplotlib import scale as mscale
from matplotlib import transforms as mtransforms
from numpy import ma
from matplotlib.ticker import FixedLocator,FuncFormatter
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):
if a in range(1,5):
return np.log(a)
else:
return 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,5):
return np.exp(a)
else:
return 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()
尽管我不断遇到以下错误:
TypeError: __init__() missing 1 required positional argument: 'axis'
好像没有保存我的自定义比例。有任何想法吗? 谢谢大家
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。