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

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

.. _sphx_glr_gallery_user_interfaces_canvasagg.py:


==============
CanvasAgg demo
==============

This example shows how to use the agg backend directly to create images, which
may be of use to web application developers who want full control over their
code without using the pyplot interface to manage figures, figure closing etc.

.. note::

    It is not necessary to avoid using the pyplot interface in order to
    create figures without a graphical front-end - simply setting
    the backend to "Agg" would be sufficient.

In this example, we show how to save the contents of the agg canvas to a file,
and how to extract them to a numpy array, which can in turn be passed off
to Pillow_.  The latter functionality allows e.g. to use Matplotlib inside a
cgi-script *without* needing to write a figure to disk, and to write images in
any format supported by Pillow.

.. _Pillow: https://pillow.readthedocs.io/
.. redirect-from:: /gallery/misc/agg_buffer
.. redirect-from:: /gallery/misc/agg_buffer_to_array

.. GENERATED FROM PYTHON SOURCE LINES 26-60

.. code-block:: Python


    from PIL import Image

    import numpy as np

    from matplotlib.backends.backend_agg import FigureCanvasAgg
    from matplotlib.figure import Figure

    fig = Figure(figsize=(5, 4), dpi=100)
    # A canvas must be manually attached to the figure (pyplot would automatically
    # do it).  This is done by instantiating the canvas with the figure as
    # argument.
    canvas = FigureCanvasAgg(fig)

    # Do some plotting.
    ax = fig.add_subplot()
    ax.plot([1, 2, 3])

    # Option 1: Save the figure to a file; can also be a file-like object (BytesIO,
    # etc.).
    fig.savefig("test.png")

    # Option 2: Retrieve a memoryview on the renderer buffer, and convert it to a
    # numpy array.
    canvas.draw()
    rgba = np.asarray(canvas.buffer_rgba())
    # ... and pass it to PIL.
    im = Image.fromarray(rgba)
    # This image can then be saved to any format supported by Pillow, e.g.:
    im.save("test.bmp")

    # Uncomment this line to display the image using ImageMagick's `display` tool.
    # im.show()








.. GENERATED FROM PYTHON SOURCE LINES 61-71

.. admonition:: References

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

   - `matplotlib.backends.backend_agg.FigureCanvasAgg`
   - `matplotlib.figure.Figure`
   - `matplotlib.figure.Figure.add_subplot`
   - `matplotlib.figure.Figure.savefig` / `matplotlib.pyplot.savefig`
   - `matplotlib.axes.Axes.plot` / `matplotlib.pyplot.plot`


.. _sphx_glr_download_gallery_user_interfaces_canvasagg.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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