Skip to main content
 首页 » 编程设计

python之检查 matplotlib 中的坐标轴刻度

2025年05月04日12kevingrace

有没有一种简单的方法来检查 matplotlib 中的轴是否是对数/线性的?

如果我输入 ax.transData.__dict__(ax 是 semilogy),我得到:

{'_a': TransformWrapper(BlendedGenericTransform(IdentityTransform(),<matplotlib.scale.Log10Transform object at 0x10ffb3650>)), 
 '_b': CompositeGenericTransform(BboxTransformFrom(TransformedBbox(Bbox('array([[  0.00000000e+00,   1.00000000e+00],\n       [  2.00000000e+03,   1.00000000e+08]])'), TransformWrapper(BlendedGenericTransform(IdentityTransform(),<matplotlib.scale.Log10Transform object at 0x10ffb3650>)))), BboxTransformTo(TransformedBbox(Bbox('array([[ 0.05482517,  0.05046296],\n       [ 0.96250543,  0.95810185]])'), BboxTransformTo(TransformedBbox(Bbox('array([[ 0.,  0.],\n       [ 8.,  6.]])'), Affine2D(array([[ 80.,   0.,   0.], 
       [  0.,  80.,   0.], 
       [  0.,   0.,   1.]]))))))), 
 '_invalid': 2, 
 '_parents': <WeakValueDictionary at 4572332904>, 
 '_shorthand_name': '', 
 'input_dims': 2, 
 'output_dims': 2} 

我可以编写一个方法来检查子转换 ax.transData._a._child 是否是对数尺度的,但我不喜欢它访问私有(private)变量,而且它看起来相当不可持续,因为变量名可以改变。

请您参考如下方法:

还有(记录不完整的)函数 axis.get_scale()

scale_str = ax.get_yaxis().get_scale() 

它返回一个字符串。