使用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 (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...