【问题标题】:How to change label position of a TextBox object?如何更改 TextBox 对象的标签位置?
【发布时间】:2021-08-15 22:38:50
【问题描述】:

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

示例代码和图像取自Matplotlib docs

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()

【问题讨论】:

    标签: python matplotlib user-interface widget interactive


    【解决方案1】:

    您可以通过手动更改标签的位置来做到这一点(文字艺术家)。

    将此添加到脚本的末尾:

    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')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-19
      • 2016-01-09
      • 2019-02-14
      相关资源
      最近更新 更多