33Textbox
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
1316import numpy as np
1821t = np .arange (- 2.0 , 2.0 , 0.001 )
1922s = t ** 2
2023initial_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):
3241text_box .on_submit (submit )
3342
3443plt .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