.. 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.py: ========== Agg Buffer ========== Use backend agg to access the figure canvas as an RGB string and then convert it to an array and pass it to Pillow for rendering. .. image:: /gallery/misc/images/sphx_glr_agg_buffer_001.png :class: sphx-glr-single-img .. code-block:: default import numpy as np from matplotlib.backends.backend_agg import FigureCanvasAgg import matplotlib.pyplot as plt plt.plot([1, 2, 3]) canvas = plt.get_current_fig_manager().canvas agg = canvas.switch_backends(FigureCanvasAgg) agg.draw() s, (width, height) = agg.print_to_buffer() # Convert to a NumPy array. X = np.frombuffer(s, np.uint8).reshape((height, width, 4)) # Pass off to PIL. from PIL import Image im = Image.frombytes("RGBA", (width, height), s) # Uncomment this line to display the image using ImageMagick's `display` tool. # im.show() .. _sphx_glr_download_gallery_misc_agg_buffer.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download :download:`Download Python source code: agg_buffer.py ` .. container:: sphx-glr-download :download:`Download Jupyter notebook: agg_buffer.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature Keywords: matplotlib code example, codex, python plot, pyplot `Gallery generated by Sphinx-Gallery `_