

.. _sphx_glr_gallery_user_interfaces_histogram_demo_canvasagg_sgskip.py:


========================
Histogram Demo CanvasAgg
========================

This is an example that shows you how to work directly with the agg
figure canvas to create a figure using the pythonic API.

In this example, the contents of the agg canvas are extracted to a
string, which can in turn be passed off to PIL or put in a numeric
array





.. code-block:: python

    from matplotlib.backends.backend_agg import FigureCanvasAgg
    from matplotlib.figure import Figure
    from matplotlib.mlab import normpdf
    import numpy as np

    fig = Figure(figsize=(5, 4), dpi=100)
    ax = fig.add_subplot(111)

    canvas = FigureCanvasAgg(fig)

    mu, sigma = 100, 15
    x = mu + sigma * np.random.randn(10000)

    # the histogram of the data
    n, bins, patches = ax.hist(x, 50, normed=1)

    # add a 'best fit' line
    y = normpdf(bins, mu, sigma)
    line, = ax.plot(bins, y, 'r--')
    line.set_linewidth(1)

    ax.set_xlabel('Smarts')
    ax.set_ylabel('Probability')
    ax.set_title(r'$\mathrm{Histogram of IQ: }\mu=100, \sigma=15$')

    ax.set_xlim((40, 160))
    ax.set_ylim((0, 0.03))

    canvas.draw()

    s = canvas.tostring_rgb()  # save this and convert to bitmap as needed

    # Get the figure dimensions for creating bitmaps or NumPy arrays,
    # etc.
    l, b, w, h = fig.bbox.bounds
    w, h = int(w), int(h)

    if 0:
        # Convert to a NumPy array
        X = np.fromstring(s, np.uint8).reshape((h, w, 3))

    if 0:
        # pass off to PIL
        from PIL import Image
        im = Image.fromstring("RGB", (w, h), s)
        im.show()

**Total running time of the script:** ( 0 minutes  0.000 seconds)



.. container:: sphx-glr-footer


  .. container:: sphx-glr-download

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



  .. container:: sphx-glr-download

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

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

    `Generated by Sphinx-Gallery <https://sphinx-gallery.readthedocs.io>`_
