|
3 | 3 | Textbox
|
4 | 4 | =======
|
5 | 5 |
|
6 |
| -Allowing text input with the Textbox widget. |
7 |
| -
|
8 |
| -You can use the Textbox widget to let users interactively provide any text |
9 |
| -that needs to be displayed, including formulas. You can use a submit button to |
10 |
| -create plots with the given input. |
11 |
| -
|
12 |
| -Note to not get confused with |
13 |
| -:doc:`/tutorials/text/annotations` and |
14 |
| -:doc:`/gallery/recipes/placing_text_boxes`, both of which are static elements. |
| 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 user |
| 9 | +presses enter in the textbox or leaves the textbox. |
| 10 | +
|
| 11 | +Note: The `Textbox` widget is different from the following static elements: |
| 12 | +:doc:`/tutorials/text/annotations` and |
| 13 | +:doc:`/gallery/recipes/placing_text_boxes`. |
15 | 14 | """
|
16 | 15 |
|
17 | 16 | import numpy as np
|
|
22 | 21 | t = np.arange(-2.0, 2.0, 0.001)
|
23 | 22 | s = t ** 2
|
24 | 23 | initial_text = "t ** 2"
|
25 |
| -l, = plt.plot(t, s, lw=2) |
| 24 | +l, = plt.plot(t, s, lw=2) # make a plot for the math expression "t ** 2" |
| 25 | + |
26 | 26 |
|
| 27 | +def submit(expression): |
| 28 | + """ |
| 29 | + Update the plotted function to the new math *expression*. |
27 | 30 |
|
28 |
| -def submit(text): |
29 |
| - # user can enter a new math expression that uses "t" as its independent |
30 |
| - # variable. The plot refreshes on entering. Example: try "t ** 3" |
31 |
| - ydata = eval(text) |
| 31 | + *expession* is a string using "t" as its independent variable, e.g. |
| 32 | + "t ** 3". |
| 33 | + """ |
| 34 | + ydata = eval(expression) |
32 | 35 | l.set_ydata(ydata)
|
33 | 36 | ax.set_ylim(np.min(ydata), np.max(ydata))
|
34 | 37 | plt.draw()
|
|
0 commit comments