.. only:: html
.. note::
:class: sphx-glr-download-link-note
Click :ref:`here ` 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.
.. image:: /gallery/misc/images/sphx_glr_agg_buffer_to_array_001.png
:alt: agg buffer to array
:class: sphx-glr-single-img
.. code-block:: default
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.figure import Figure
from matplotlib.backends.backend_agg import FigureCanvas
# Create a figure that pyplot does not know about.
fig = Figure()
# attach a non-interactive Agg canvas to the figure
# (as a side-effect of the ``__init__``)
canvas = FigureCanvas(fig)
ax = fig.subplots()
ax.plot([1, 2, 3])
ax.set_title('a simple figure')
# Force a draw so we can grab the pixel buffer
canvas.draw()
# grab the pixel buffer and dump it into a numpy array
X = np.array(canvas.renderer.buffer_rgba())
# now display the array X as an Axes in a new figure
fig2 = plt.figure()
ax2 = fig2.add_subplot(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 sphx-glr-download-python
:download:`Download Python source code: agg_buffer_to_array.py `
.. container:: sphx-glr-download sphx-glr-download-jupyter
:download:`Download Jupyter notebook: 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
`_