Skip to content

Commit 2bdf82b

Browse files
authored
Merge pull request #16549 from chrissshe/improve_textbox_example
Improve documentation for examples/widgets/textbox.py
2 parents 8941aee + 4f6b9f3 commit 2bdf82b

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

.flake8

+1
Original file line numberDiff line numberDiff line change
@@ -262,3 +262,4 @@ per-file-ignores =
262262
examples/userdemo/connectionstyle_demo.py: E402
263263
examples/userdemo/custom_boxstyle01.py: E402
264264
examples/userdemo/pgf_preamble_sgskip.py: E402
265+
examples/widgets/textbox.py: E402

examples/widgets/textbox.py

+28-7
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
Textbox
44
=======
55
6-
Allowing text input with the Textbox widget.
6+
The Textbox widget lets users interactively provide text input, including
7+
formulas. In this example, the plot is updated using the `.on_submit` method.
8+
This method triggers the execution of the *submit* function when the
9+
user presses enter in the textbox or leaves the textbox.
710
8-
You can use the Textbox widget to let users provide any text that needs to be
9-
displayed, including formulas. You can use a submit button to create plots
10-
with the given input.
11+
Note: The `matplotlib.widgets.TextBox` widget is different from the following
12+
static elements: :doc:`/tutorials/text/annotations` and
13+
:doc:`/gallery/recipes/placing_text_boxes`.
1114
"""
1215

1316
import numpy as np
@@ -18,11 +21,17 @@
1821
t = np.arange(-2.0, 2.0, 0.001)
1922
s = t ** 2
2023
initial_text = "t ** 2"
21-
l, = plt.plot(t, s, lw=2)
24+
l, = plt.plot(t, s, lw=2) # make a plot for the math expression "t ** 2"
2225

2326

24-
def submit(text):
25-
ydata = eval(text)
27+
def submit(expression):
28+
"""
29+
Update the plotted function to the new math *expression*.
30+
31+
*expession* is a string using "t" as its independent variable, e.g.
32+
"t ** 3".
33+
"""
34+
ydata = eval(expression)
2635
l.set_ydata(ydata)
2736
ax.set_ylim(np.min(ydata), np.max(ydata))
2837
plt.draw()
@@ -32,3 +41,15 @@ def submit(text):
3241
text_box.on_submit(submit)
3342

3443
plt.show()
44+
45+
#############################################################################
46+
#
47+
# ------------
48+
#
49+
# References
50+
# """"""""""
51+
#
52+
# The use of the following functions, methods, classes and modules is shown
53+
# in this example:
54+
55+
from matplotlib.widgets import TextBox

0 commit comments

Comments
 (0)