Home > other >  Get Matplotlib axis spine position in relative screen coordinates?
Get Matplotlib axis spine position in relative screen coordinates?

Time:01-28

Consider this example:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Button

freqs = np.arange(2, 20, 3)

fig, ax = plt.subplots()
plt.subplots_adjust(bottom=0.2)
t = np.arange(0.0, 1.0, 0.001)
s = np.sin(2*np.pi*freqs[0]*t)
l, = plt.plot(t, s, lw=2)

left, bottom, width, height = 0.2, 0.91, 0.09, 0.05
btn = Button(plt.axes([left, bottom, width, height]), "Hello!")

plt.show()

This produces:

enter image description here

How can I get the actual position of the left spine of the axis in screen coordinates (relative, from 0.0 to 1.0) - so that I could set left to it, and the button would align with the red line indicated on the screenshot?

CodePudding user response:

You could use the enter image description here

As a side effect, the button size will change when resizing the window. Whether this is a wanted or unwanted effect is up to your judgment.

CodePudding user response:

I played around with the spline properties and I think I found what you need. If you print(ax.spines['left']get_spine_transform()) you get

BlendedGenericTransform(
    BboxTransformTo(
        TransformedBbox(
            Bbox(x0=0.125, y0=0.20000000000000007, x1=0.9, y1=0.88),
            BboxTransformTo(
                TransformedBbox(
                    Bbox(x0=0.0, y0=0.0, x1=6.4, y1=4.8),
                    Affine2D().scale(100.0))))),
    CompositeGenericTransform(
        TransformWrapper(
            BlendedAffine2D(
                IdentityTransform(),
                IdentityTransform())),
        CompositeGenericTransform(
            BboxTransformFrom(
                TransformedBbox(
                    Bbox(x0=-0.04995, y0=-1.1, x1=1.04895, y1=1.1),
                    TransformWrapper(
                        BlendedAffine2D(
                            IdentityTransform(),
                            IdentityTransform())))),
            BboxTransformTo(
                TransformedBbox(
                    Bbox(x0=0.125, y0=0.20000000000000007, x1=0.9, y1=0.88),
                    BboxTransformTo(
                        TransformedBbox(
                            Bbox(x0=0.0, y0=0.0, x1=6.4, y1=4.8),
                            Affine2D().scale(100.0))))))))

It looks like 0.125 is the value you are looking for. I am not sure how the trnaform structure works with matplotlib,so I don't know how to actually get that value contians in

BlendedGenericTransform(
    BboxTransformTo(
        TransformedBbox(
            Bbox(

So this answer isn't quite complete, but hopefully a helpful starting point.

  •  Tags:  
  • Related