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

    Click :ref:`here <sphx_glr_download_gallery_misc_agg_buffer_to_array.py>` to download the full example code
.. rst-class:: sphx-glr-example-title

.. _sphx_glr_gallery_misc_agg_buffer_to_array.py:


===================
Agg Buffer To Array
===================

Convert a rendered figure to its image (NumPy array) representation.




.. rst-class:: sphx-glr-horizontal


    *

      .. image:: /gallery/misc/images/sphx_glr_agg_buffer_to_array_001.png
            :class: sphx-glr-multi-img

    *

      .. image:: /gallery/misc/images/sphx_glr_agg_buffer_to_array_002.png
            :class: sphx-glr-multi-img





.. code-block:: python

    import matplotlib.pyplot as plt
    import numpy as np

    # make an agg figure
    fig, ax = plt.subplots()
    ax.plot([1, 2, 3])
    ax.set_title('a simple figure')
    fig.canvas.draw()

    # grab the pixel buffer and dump it into a numpy array
    X = np.array(fig.canvas.renderer._renderer)

    # now display the array X as an Axes in a new figure
    fig2 = plt.figure()
    ax2 = fig2.add_subplot(111, frameon=False)
    ax2.imshow(X)
    plt.show()


.. _sphx_glr_download_gallery_misc_agg_buffer_to_array.py:


.. only :: html

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



  .. container:: sphx-glr-download

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



  .. container:: sphx-glr-download

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


.. only:: html

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

    Keywords: matplotlib code example, codex, python plot, pyplot
    `Gallery generated by Sphinx-Gallery
    <https://sphinx-gallery.readthedocs.io>`_
