
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "gallery/widgets/textbox.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. meta::
        :keywords: codex

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_gallery_widgets_textbox.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_gallery_widgets_textbox.py:


=======
Textbox
=======

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.

Note:  The `matplotlib.widgets.TextBox` widget is different from the following
static elements: :ref:`annotations` and
:doc:`/gallery/text_labels_and_annotations/placing_text_boxes`.

.. GENERATED FROM PYTHON SOURCE LINES 15-49

.. code-block:: Python


    import matplotlib.pyplot as plt
    import numpy as np

    from matplotlib.widgets import TextBox

    fig, ax = plt.subplots()
    fig.subplots_adjust(bottom=0.2)

    t = np.arange(-2.0, 2.0, 0.001)
    l, = ax.plot(t, np.zeros_like(t), lw=2)


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

        *expression* is a string using "t" as its independent variable, e.g.
        "t ** 3".
        """
        ydata = eval(expression, {'np': np}, {'t': t})
        l.set_ydata(ydata)
        ax.relim()
        ax.autoscale_view()
        plt.draw()


    axbox = fig.add_axes([0.1, 0.05, 0.8, 0.075])
    text_box = TextBox(axbox, "Evaluate", textalignment="center")
    text_box.on_submit(submit)
    text_box.set_val("t ** 2")  # Trigger `submit` with the initial string.

    plt.show()




.. image-sg:: /gallery/widgets/images/sphx_glr_textbox_001.png
   :alt: textbox
   :srcset: /gallery/widgets/images/sphx_glr_textbox_001.png, /gallery/widgets/images/sphx_glr_textbox_001_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 50-56

.. admonition:: References

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

   - `matplotlib.widgets.TextBox`


.. _sphx_glr_download_gallery_widgets_textbox.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: textbox.ipynb <textbox.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: textbox.py <textbox.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: textbox.zip <textbox.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
