{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Textbox\n\nThe Textbox widget lets users interactively provide text input, including\nformulas. In this example, the plot is updated using the `.on_submit` method.\nThis method triggers the execution of the *submit* function when the\nuser presses enter in the textbox or leaves the textbox.\n\nNote: The `matplotlib.widgets.TextBox` widget is different from the following\nstatic elements: `annotations` and\n:doc:`/gallery/text_labels_and_annotations/placing_text_boxes`.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\n\nfrom matplotlib.widgets import TextBox\n\nfig, ax = plt.subplots()\nfig.subplots_adjust(bottom=0.2)\n\nt = np.arange(-2.0, 2.0, 0.001)\nl, = ax.plot(t, np.zeros_like(t), lw=2)\n\n\ndef submit(expression):\n \"\"\"\n Update the plotted function to the new math *expression*.\n\n *expression* is a string using \"t\" as its independent variable, e.g.\n \"t ** 3\".\n \"\"\"\n ydata = eval(expression, {'np': np}, {'t': t})\n l.set_ydata(ydata)\n ax.relim()\n ax.autoscale_view()\n plt.draw()\n\n\naxbox = fig.add_axes([0.1, 0.05, 0.8, 0.075])\ntext_box = TextBox(axbox, \"Evaluate\", textalignment=\"center\")\ntext_box.on_submit(submit)\ntext_box.set_val(\"t ** 2\") # Trigger `submit` with the initial string.\n\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ".. admonition:: References\n\n The use of the following functions, methods, classes and modules is shown\n in this example:\n\n - `matplotlib.widgets.TextBox`\n\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.13.2" } }, "nbformat": 4, "nbformat_minor": 0 }