Sophie

Sophie

distrib > Fedora > 18 > i386 > by-pkgid > bf21b4394f4d7fa09e3626145d3315e0 > files > 354

python-matplotlib-doc-1.2.0-14.fc18.i686.rpm

.. _pylab_examples-errorbar_demo:

pylab_examples example code: errorbar_demo.py
=============================================



.. plot:: /builddir/build/BUILD/matplotlib-1.2.0/doc/mpl_examples/pylab_examples/errorbar_demo.py

::

    #!/usr/bin/env python
    import numpy as np
    import matplotlib.pyplot as plt
    
    # example data
    x = np.arange(0.1, 4, 0.5)
    y = np.exp(-x)
    
    # example variable error bar values
    yerr = 0.1 + 0.2*np.sqrt(x)
    xerr = 0.1 + yerr
    
    # First illustrate basic pyplot interface, using defaults where possible.
    plt.figure()
    plt.errorbar(x, y, xerr=0.2, yerr=0.4)
    plt.title("Simplest errorbars, 0.2 in x, 0.4 in y")
    
    # Now switch to a more OO interface to exercise more features.
    fig, axs = plt.subplots(nrows=2, ncols=2, sharex=True)
    ax = axs[0,0]
    ax.errorbar(x, y, yerr=yerr, fmt='o')
    ax.set_title('Vert. symmetric')
    
    # With 4 subplots, reduce the number of axis ticks to avoid crowding.
    ax.locator_params(nbins=4)
    
    ax = axs[0,1]
    ax.errorbar(x, y, xerr=xerr, fmt='o')
    ax.set_title('Hor. symmetric')
    
    ax = axs[1,0]
    ax.errorbar(x, y, yerr=[yerr, 2*yerr], xerr=[xerr, 2*xerr], fmt='--o')
    ax.set_title('H, V asymmetric')
    
    ax = axs[1,1]
    ax.set_yscale('log')
    # Here we have to be careful to keep all y values positive:
    ylower = np.maximum(1e-2, y - yerr)
    yerr_lower = y - ylower
    
    ax.errorbar(x, y, yerr=[yerr_lower, 2*yerr], xerr=xerr,
                fmt='o', ecolor='g', capthick=2)
    ax.set_title('Mixed sym., log y')
    
    fig.suptitle('Variable errorbars')
    
    plt.show()
    
    

Keywords: python, matplotlib, pylab, example, codex (see :ref:`how-to-search-examples`)