Olga Botvinnik
Olga Botvinnik

Reputation: 1644

Check axes scale in matplotlib

Is there an easy way to check if axes in matplotlib are logarithmic/linear?

If I type ax.transData.__dict__ (ax is semilogy), I get:

{'_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}

I could write a method to check if the subtransforms ax.transData._a._child are log-scale but I don't like that it accesses private variables and it seems rather unsustainable, since the variable name can change.

Upvotes: 2

Views: 1159

Answers (2)

tacaswell
tacaswell

Reputation: 87376

There is also the (poorly documented) function axis.get_scale()

scale_str = ax.get_yaxis().get_scale()

which returns a string.

Upvotes: 7

Olga Botvinnik
Olga Botvinnik

Reputation: 1644

Turns out the scale is hidden in ax.yaxis._scale:

import matplotlib as mpl
type(ax.yaxis._scale) == mpl.scale.LogScale

This returns True, which is exactly what I need.

Upvotes: 3

Related Questions