微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何更改 TextBox 对象的标签位置?

如何解决如何更改 TextBox 对象的标签位置?

TextBox 标签认位置在其左侧,如图所示。我想把标签放在盒子下面。这可能吗?

示例代码和图像取自 Matplotlib docs

enter image description here

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


fig,ax = plt.subplots()
fig.subplots_adjust(bottom=0.2)

t = np.arange(-2.0,2.0,0.001)
l,= ax.plot(t,np.zeros_like(t),lw=2)


def submit(expression):
    """
    Update the plotted function to the new math *expression*.

    *expression* is a string using "t" as its independent variable,e.g.
    "t ** 3".
    """
    ydata = eval(expression)
    l.set_ydata(ydata)
    ax.relim()
    ax.autoscale_view()
    plt.draw()


axBox = fig.add_axes([0.1,0.05,0.8,0.075])
text_Box = TextBox(axBox,"Evaluate")
text_Box.on_submit(submit)
text_Box.set_val("t ** 2")  # Trigger `submit` with the initial string.

plt.show()

解决方法

您可以通过手动更改标签的位置(文本艺术家)来实现。

将此添加到脚本的末尾:

label = text_box.ax.get_children()[1] # label is a child of the TextBox axis
label.set_position([0.5,-.1]) # [x,y] - change here to set the position
# centering the text
label.set_verticalalignment('top')
label.set_horizontalalignment('center')

Sample TextBox with label below the box

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。