注解
Click here 下载完整的示例代码
文本框¶
Textbox小部件允许用户交互式地提供文本输入,包括公式。在本例中,使用 on_submit
方法。此方法触发 提交 当用户在文本框中按enter键或离开文本框时。
注:以下为 matplotlib.widgets.TextBox
小部件不同于以下静态元素: 注解 和 放置文本框 .
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()

工具书类¶
以下函数、方法、类和模块的使用如本例所示:
from matplotlib.widgets import TextBox
关键词:matplotlib代码示例,codex,python plot,pyplot Gallery generated by Sphinx-Gallery