3
3
Textbox
4
4
=======
5
5
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.
7
10
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` .
11
14
"""
12
15
13
16
import numpy as np
18
21
t = np .arange (- 2.0 , 2.0 , 0.001 )
19
22
s = t ** 2
20
23
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"
22
25
23
26
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 )
26
35
l .set_ydata (ydata )
27
36
ax .set_ylim (np .min (ydata ), np .max (ydata ))
28
37
plt .draw ()
@@ -32,3 +41,15 @@ def submit(text):
32
41
text_box .on_submit (submit )
33
42
34
43
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