简体   繁体   English

在相对屏幕坐标中获取 Matplotlib 轴脊 position?

[英]Get Matplotlib axis spine position in relative screen coordinates?

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:这会产生:

在此处输入图像描述

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?如何在屏幕坐标(相对,从 0.0 到 1.0)中获得轴的左脊柱的实际 position - 这样我就可以将其设置为left ,并且按钮将与屏幕截图上指示的红线对齐?

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如果你print(ax.spines['left']get_spine_transform())你得到

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.看起来 0.125 是您正在寻找的值。 I am not sure how the trnaform structure works with matplotlib,so I don't know how to actually get that value contians in我不确定 trnaform 结构如何与 matplotlib 一起使用,所以我不知道如何实际获取该值 contians

BlendedGenericTransform(
    BboxTransformTo(
        TransformedBbox(
            Bbox(

So this answer isn't quite complete, but hopefully a helpful starting point.所以这个答案并不完整,但希望是一个有用的起点。

You could use the InsetPosition helper class provided by mpl_toolkits.axes_grid1:您可以使用mpl_toolkits.axes_grid1 提供的 InsetPosition 助手class:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Button
from mpl_toolkits.axes_grid1.inset_locator import InsetPosition

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

fig, ax = plt.subplots(figsize=(10,8))
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, = ax.plot(t, s, lw=2)

left, bottom, width, height = 0, 1.05, 0.09, 0.05
button_ax = plt.axes([0, 0, 1, 1])
ip = InsetPosition(ax, [left, bottom, width, height]) 
button_ax.set_axes_locator(ip)
btn = Button(button_ax, "Hello!")

plt.show()

Sample output:样品 output: 在此处输入图像描述

As a side effect, the button size will change when resizing the window.作为副作用,按钮大小会在调整 window 的大小时发生变化。 Whether this is a wanted or unwanted effect is up to your judgment.这是想要的还是不想要的效果取决于您的判断。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM