
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "gallery/text_labels_and_annotations/mathtext_asarray.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_text_labels_and_annotations_mathtext_asarray.py>`
        to download the full example code.

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

.. _sphx_glr_gallery_text_labels_and_annotations_mathtext_asarray.py:


=======================
Convert texts to images
=======================

.. GENERATED FROM PYTHON SOURCE LINES 6-50

.. code-block:: Python


    from io import BytesIO

    import matplotlib.pyplot as plt

    from matplotlib.figure import Figure
    from matplotlib.transforms import IdentityTransform


    def text_to_rgba(s, *, dpi, **kwargs):
        # To convert a text string to an image, we can:
        # - draw it on an empty and transparent figure;
        # - save the figure to a temporary buffer using ``bbox_inches="tight",
        #   pad_inches=0`` which will pick the correct area to save;
        # - load the buffer using ``plt.imread``.
        #
        # (If desired, one can also directly save the image to the filesystem.)
        fig = Figure(facecolor="none")
        fig.text(0, 0, s, **kwargs)
        with BytesIO() as buf:
            fig.savefig(buf, dpi=dpi, format="png", bbox_inches="tight",
                        pad_inches=0)
            buf.seek(0)
            rgba = plt.imread(buf)
        return rgba


    fig = plt.figure()
    rgba1 = text_to_rgba(r"IQ: $\sigma_i=15$", color="blue", fontsize=20, dpi=200)
    rgba2 = text_to_rgba(r"some other string", color="red", fontsize=20, dpi=200)
    # One can then draw such text images to a Figure using `.Figure.figimage`.
    fig.figimage(rgba1, 100, 50)
    fig.figimage(rgba2, 100, 150)

    # One can also directly draw texts to a figure with positioning
    # in pixel coordinates by using `.Figure.text` together with
    # `.transforms.IdentityTransform`.
    fig.text(100, 250, r"IQ: $\sigma_i=15$", color="blue", fontsize=20,
             transform=IdentityTransform())
    fig.text(100, 350, r"some other string", color="red", fontsize=20,
             transform=IdentityTransform())

    plt.show()




.. image-sg:: /gallery/text_labels_and_annotations/images/sphx_glr_mathtext_asarray_001.png
   :alt: mathtext asarray
   :srcset: /gallery/text_labels_and_annotations/images/sphx_glr_mathtext_asarray_001.png, /gallery/text_labels_and_annotations/images/sphx_glr_mathtext_asarray_001_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 51-60

.. admonition:: References

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

   - `matplotlib.figure.Figure.figimage`
   - `matplotlib.figure.Figure.text`
   - `matplotlib.transforms.IdentityTransform`
   - `matplotlib.image.imread`


.. _sphx_glr_download_gallery_text_labels_and_annotations_mathtext_asarray.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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