Skip to content

Improve documentation for examples/widgets/textbox.py #16549

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -262,3 +262,4 @@ per-file-ignores =
examples/userdemo/connectionstyle_demo.py: E402
examples/userdemo/custom_boxstyle01.py: E402
examples/userdemo/pgf_preamble_sgskip.py: E402
examples/widgets/textbox.py: E402
35 changes: 28 additions & 7 deletions examples/widgets/textbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
Textbox
=======

Allowing text input with the Textbox widget.
The Textbox widget lets users interactively provide text input, including
formulas. In this example, the plot is updated using the `.on_submit` method.
This method triggers the execution of the *submit* function when the
user presses enter in the textbox or leaves the textbox.

You can use the Textbox widget to let users provide any text that needs to be
displayed, including formulas. You can use a submit button to create plots
with the given input.
Note: The `matplotlib.widgets.TextBox` widget is different from the following
static elements: :doc:`/tutorials/text/annotations` and
:doc:`/gallery/recipes/placing_text_boxes`.
"""

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


def submit(text):
ydata = eval(text)
def submit(expression):
"""
Update the plotted function to the new math *expression*.

*expession* is a string using "t" as its independent variable, e.g.
"t ** 3".
"""
ydata = eval(expression)
l.set_ydata(ydata)
ax.set_ylim(np.min(ydata), np.max(ydata))
plt.draw()
Expand All @@ -32,3 +41,15 @@ def submit(text):
text_box.on_submit(submit)

plt.show()

#############################################################################
#
# ------------
#
# References
# """"""""""
#
# The use of the following functions, methods, classes and modules is shown
# in this example:

from matplotlib.widgets import TextBox