Skip to content

Commit 3ffbf79

Browse files
committed
moved fill_between to axes/pyplot method
svn path=/trunk/matplotlib/; revision=6437
1 parent 50c4f72 commit 3ffbf79

File tree

8 files changed

+291
-221
lines changed

8 files changed

+291
-221
lines changed

boilerplate.py

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ def %(func)s(*args, **kwargs):
6464
'csd',
6565
'errorbar',
6666
'fill',
67+
'fill_between',
6768
'hexbin',
6869
'hist',
6970
'hlines',

doc/_templates/index.html

+13
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,19 @@ <h3>Plotting commands</h3> <br/>
409409
</td>
410410

411411
</tr>
412+
413+
<tr>
414+
<th align="left">
415+
<a href="api/pyplot_api.html#matplotlib.pyplot.fill_between">fill_between</a>
416+
417+
</th>
418+
419+
<td align="left">
420+
make filled polygons between two curves
421+
</td>
422+
423+
</tr>
424+
412425
<tr>
413426
<th align="left">
414427
<a href="api/pyplot_api.html#matplotlib.pyplot.findobj">findobj</a>

examples/api/fill_where_demo.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
fig = plt.figure()
1818
ax = fig.add_subplot(111)
1919
ax.set_title('using fill_between_where')
20-
ax.plot(t, s1, t, s2)
20+
ax.plot(t, s1, t, s2, color='black')
2121
ax.axhline(0, color='black', lw=2)
2222

2323
collection = collections.PolyCollection.fill_between_where(
@@ -32,7 +32,7 @@
3232
fig = plt.figure()
3333
ax = fig.add_subplot(111)
3434
ax.set_title('using span_where')
35-
ax.plot(t, s1, '-')
35+
ax.plot(t, s1, , color='black')
3636
ax.axhline(0, color='black', lw=2)
3737

3838
collection = collections.BrokenBarHCollection.span_where(

examples/api/span_regions.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""
2+
Illustrate some helper functions for shading regions where a logical
3+
mask is True
4+
5+
See :meth:`matplotlib.collections.BrokenBarHCollection.span_where`
6+
"""
7+
import numpy as np
8+
import matplotlib.pyplot as plt
9+
import matplotlib.collections as collections
10+
11+
12+
t = np.arange(0.0, 2, 0.01)
13+
s1 = np.sin(2*np.pi*t)
14+
s2 = 1.2*np.sin(4*np.pi*t)
15+
16+
17+
fig = plt.figure()
18+
ax = fig.add_subplot(111)
19+
ax.set_title('using span_where')
20+
ax.plot(t, s1, color='black')
21+
ax.axhline(0, color='black', lw=2)
22+
23+
collection = collections.BrokenBarHCollection.span_where(
24+
t, ymin=0, ymax=1, where=s1>0, facecolor='green', alpha=0.5)
25+
ax.add_collection(collection)
26+
27+
collection = collections.BrokenBarHCollection.span_where(
28+
t, ymin=-1, ymax=0, where=s1<0, facecolor='red', alpha=0.5)
29+
ax.add_collection(collection)
30+
31+
32+
33+
plt.show()
34+
35+
36+
37+
38+

examples/pylab_examples/fill_between.py

+17-13
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,31 @@
33
from pylab import figure, show
44
import numpy as np
55

6-
x = np.arange(0, 2, 0.01)
6+
x = np.arange(0.0, 2, 0.01)
77
y1 = np.sin(2*np.pi*x)
8-
y2 = np.sin(4*np.pi*x) + 2
8+
y2 = 1.2*np.sin(4*np.pi*x)
99

1010
fig = figure()
11-
ax = fig.add_subplot(311)
12-
ax2 = fig.add_subplot(312)
13-
ax3 = fig.add_subplot(313)
11+
ax1 = fig.add_subplot(311)
12+
ax2 = fig.add_subplot(312, sharex=ax1)
13+
ax3 = fig.add_subplot(313, sharex=ax1)
1414

15+
ax1.fill_between(x, 0, y1)
16+
ax1.set_ylabel('between y1 and 0')
1517

16-
xs, ys = mlab.poly_between(x, 0, y1)
17-
ax.fill(xs, ys)
18-
ax.set_ylabel('between y1 and 0')
19-
20-
xs, ys = mlab.poly_between(x, y1, 1)
21-
ax2.fill(xs, ys)
18+
ax2.fill_between(x, y1, 1)
2219
ax2.set_ylabel('between y1 and 1')
2320

24-
xs, ys = mlab.poly_between(x, y1, y2)
25-
ax3.fill(xs, ys)
21+
ax3.fill_between(x, y1, y2)
2622
ax3.set_ylabel('between y1 and y2')
2723
ax3.set_xlabel('x')
24+
25+
fig = figure()
26+
ax = fig.add_subplot(111)
27+
ax1.plot(x, y1, x, y2, color='black')
28+
ax.fill_between(x, y1, y2, where=y2>y1, facecolor='green')
29+
ax.fill_between(x, y1, y2, where=y2<=y1, facecolor='red')
30+
ax.set_title('fill between where')
31+
2832
show()
2933

lib/matplotlib/axes.py

+88-8
Original file line numberDiff line numberDiff line change
@@ -1302,6 +1302,7 @@ def add_collection(self, collection, autolim=True):
13021302
if autolim:
13031303
if collection._paths and len(collection._paths):
13041304
self.update_datalim(collection.get_datalim(self.transData))
1305+
13051306
collection._remove_method = lambda h: self.collections.remove(h)
13061307

13071308
def add_line(self, line):
@@ -5456,11 +5457,7 @@ def fill(self, *args, **kwargs):
54565457
supports are supported by the fill format string.
54575458
54585459
If you would like to fill below a curve, eg. shade a region
5459-
between 0 and *y* along *x*, use
5460-
:func:`~matplotlib.pylab.poly_between`, eg.::
5461-
5462-
xs, ys = poly_between(x, 0, y)
5463-
axes.fill(xs, ys, facecolor='red', alpha=0.5)
5460+
between 0 and *y* along *x*, use :meth:`fill_between`
54645461
54655462
The *closed* kwarg will close the polygon when *True* (default).
54665463
@@ -5472,9 +5469,6 @@ def fill(self, *args, **kwargs):
54725469
54735470
.. plot:: mpl_examples/pylab_examples/fill_demo.py
54745471
5475-
.. seealso::
5476-
:file:`examples/pylab_examples/fill_between.py`:
5477-
For more examples.
54785472
"""
54795473
if not self._hold: self.cla()
54805474

@@ -5486,6 +5480,92 @@ def fill(self, *args, **kwargs):
54865480
return patches
54875481
fill.__doc__ = cbook.dedent(fill.__doc__) % martist.kwdocd
54885482

5483+
def fill_between(self, x, y1, y2=0, where=None, **kwargs):
5484+
"""
5485+
call signature::
5486+
5487+
fill_between(x, y1, y2=0, where=None, **kwargs)
5488+
5489+
Create a :class:`~matplotlib.collectionsPolyCollection`
5490+
filling the regions between *y1* and *y2* where
5491+
``where==True``
5492+
5493+
*x*
5494+
an N length np array of the x data
5495+
5496+
*y1*
5497+
an N length scalar or np array of the x data
5498+
5499+
*y2*
5500+
an N length scalar or np array of the x data
5501+
5502+
*where*
5503+
if None, default to fill between everywhere. If not None,
5504+
it is a a N length numpy boolean array and the fill will
5505+
only happen over the regions where ``where==True``
5506+
5507+
*kwargs*
5508+
keyword args passed on to the :class:`PolyCollection`
5509+
5510+
.. seealso::
5511+
:file:`examples/pylab_examples/fill_between.py`:
5512+
For more examples.
5513+
5514+
kwargs control the Polygon properties:
5515+
5516+
%(PolyCollection)s
5517+
5518+
"""
5519+
x = np.asarray(x)
5520+
if not cbook.iterable(y1):
5521+
y1 = np.ones_like(x)*y1
5522+
5523+
if not cbook.iterable(y2):
5524+
y2 = np.ones_like(x)*y2
5525+
5526+
if where is None:
5527+
where = np.ones(len(x), np.bool)
5528+
5529+
y1 = np.asarray(y1)
5530+
y2 = np.asarray(y2)
5531+
where = np.asarray(where)
5532+
assert( (len(x)==len(y1)) and (len(x)==len(y2)) and len(x)==len(where))
5533+
5534+
polys = []
5535+
for ind0, ind1 in mlab.contiguous_regions(where):
5536+
theseverts = []
5537+
xslice = x[ind0:ind1]
5538+
y1slice = y1[ind0:ind1]
5539+
y2slice = y2[ind0:ind1]
5540+
5541+
if not len(xslice):
5542+
continue
5543+
5544+
N = len(xslice)
5545+
X = np.zeros((2*N+2, 2), np.float)
5546+
5547+
# the purpose of the next two lines is for when y2 is a
5548+
# scalar like 0 and we want the fill to go all the way
5549+
# down to 0 even if none of the y1 sample points do
5550+
X[0] = xslice[0], y2slice[0]
5551+
X[N+1] = xslice[-1], y2slice[-1]
5552+
5553+
X[1:N+1,0] = xslice
5554+
X[1:N+1,1] = y1slice
5555+
X[N+2:,0] = xslice[::-1]
5556+
X[N+2:,1] = y2slice[::-1]
5557+
5558+
polys.append(X)
5559+
5560+
collection = mcoll.PolyCollection(polys, **kwargs)
5561+
5562+
self.update_datalim_numerix(x[where], y1[where])
5563+
self.update_datalim_numerix(x[where], y2[where])
5564+
self.add_collection(collection)
5565+
self.autoscale_view()
5566+
return collection
5567+
fill_between.__doc__ = cbook.dedent(fill_between.__doc__) % martist.kwdocd
5568+
54895569
#### plotting z(x,y): imshow, pcolor and relatives, contour
54905570

54915571
def imshow(self, X, cmap=None, norm=None, aspect=None,

lib/matplotlib/collections.py

-60
Original file line numberDiff line numberDiff line change
@@ -673,66 +673,6 @@ def draw(self, renderer):
673673
return Collection.draw(self, renderer)
674674

675675

676-
@staticmethod
677-
def fill_between_where(x, y1, y2, where, **kwargs):
678-
"""
679-
Create a :class:`PolyCollection` filling the regions between *y*
680-
and *yboundary7* where ``where==True``
681-
682-
683-
*x*
684-
an N length np array of the x data
685-
686-
*y1*
687-
an N length scalar or np array of the x data
688-
689-
*y2*
690-
an N length scalar or np array of the x data
691-
692-
*where*
693-
an N length numpy boolean array
694-
695-
*kwargs*
696-
keyword args passed on to the :class:`PolyCollection`
697-
698-
"""
699-
if not cbook.iterable(y1):
700-
y1 = np.ones_like(x)*y1
701-
702-
if not cbook.iterable(y2):
703-
y2 = np.ones_like(x)*y2
704-
705-
assert( (len(x)==len(y1)) and (len(x)==len(y2)) )
706-
707-
polys = []
708-
for ind0, ind1 in mlab.contiguous_regions(where):
709-
theseverts = []
710-
xslice = x[ind0:ind1]
711-
y1slice = y1[ind0:ind1]
712-
y2slice = y2[ind0:ind1]
713-
714-
if not len(xslice):
715-
continue
716-
717-
N = len(xslice)
718-
X = np.zeros((2*N+2, 2), np.float)
719-
720-
# the purpose of the next two lines is for when y2 is a
721-
# scalar like 0 and we want the fill to go all the way
722-
# down to 0 even if none of the y1 sample points do
723-
X[0] = xslice[0], y2slice[0]
724-
X[N+1] = xslice[-1], y2slice[-1]
725-
726-
X[1:N+1,0] = xslice
727-
X[1:N+1,1] = y1slice
728-
X[N+2:,0] = xslice[::-1]
729-
X[N+2:,1] = y2slice[::-1]
730-
731-
polys.append(X)
732-
733-
collection = PolyCollection(polys, **kwargs)
734-
return collection
735-
736676
class BrokenBarHCollection(PolyCollection):
737677
"""
738678
A collection of horizontal bars spanning *yrange* with a sequence of

0 commit comments

Comments
 (0)