.. _pylab_examples-fill_between:

pylab_examples example code: fill_between.py
============================================



.. plot:: /build/buildd/matplotlib-0.98.5.2/doc/mpl_examples/pylab_examples/fill_between.py

::

    #!/usr/bin/env python
    import matplotlib.mlab as mlab
    from pylab import figure, show
    import numpy as np
    
    x = np.arange(0.0, 2, 0.01)
    y1 = np.sin(2*np.pi*x)
    y2 = 1.2*np.sin(4*np.pi*x)
    
    fig = figure()
    ax1 = fig.add_subplot(311)
    ax2 = fig.add_subplot(312, sharex=ax1)
    ax3 = fig.add_subplot(313, sharex=ax1)
    
    ax1.fill_between(x, 0, y1)
    ax1.set_ylabel('between y1 and 0')
    
    ax2.fill_between(x, y1, 1)
    ax2.set_ylabel('between y1 and 1')
    
    ax3.fill_between(x, y1, y2)
    ax3.set_ylabel('between y1 and y2')
    ax3.set_xlabel('x')
    
    # now fill between y1 and y2 where a logical condition is met.  Note
    # this is different than calling
    #   fill_between(x[where], y1[where],y2[where]
    # because of edge effects over multiple contiguous regions.
    fig = figure()
    ax = fig.add_subplot(111)
    ax.plot(x, y1, x, y2, color='black')
    ax.fill_between(x, y1, y2, where=y2>y1, facecolor='green')
    ax.fill_between(x, y1, y2, where=y2<=y1, facecolor='red')
    ax.set_title('fill between where')
    
    show()
    
    

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