Skip to content

Commit da5181d

Browse files
committed
Condense argument handling in fill_between, and add masked example
svn path=/trunk/matplotlib/; revision=7071
1 parent 71dc0e6 commit da5181d

File tree

2 files changed

+32
-30
lines changed

2 files changed

+32
-30
lines changed

examples/pylab_examples/fill_between.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python
22
import matplotlib.mlab as mlab
3-
from pylab import figure, show
3+
from matplotlib.pyplot import figure, show
44
import numpy as np
55

66
x = np.arange(0.0, 2, 0.01)
@@ -27,11 +27,24 @@
2727
# fill_between(x[where], y1[where],y2[where]
2828
# because of edge effects over multiple contiguous regions.
2929
fig = figure()
30-
ax = fig.add_subplot(111)
30+
ax = fig.add_subplot(211)
3131
ax.plot(x, y1, x, y2, color='black')
32-
ax.fill_between(x, y1, y2, where=y2>y1, facecolor='green')
32+
ax.fill_between(x, y1, y2, where=y2>=y1, facecolor='green')
3333
ax.fill_between(x, y1, y2, where=y2<=y1, facecolor='red')
3434
ax.set_title('fill between where')
3535

36+
# Test support for masked arrays.
37+
y2 = np.ma.masked_greater(y2, 1.0)
38+
ax1 = fig.add_subplot(212, sharex=ax)
39+
ax1.plot(x, y1, x, y2, color='black')
40+
ax1.fill_between(x, y1, y2, where=y2>=y1, facecolor='green')
41+
ax1.fill_between(x, y1, y2, where=y2<=y1, facecolor='red')
42+
ax1.set_title('Now regions with y2>1 are masked')
43+
44+
# This example illustrates a problem; because of the data
45+
# gridding, there are undesired unfilled triangles at the crossover
46+
# points. A brute-force solution would be to interpolate all
47+
# arrays to a very fine grid before plotting.
48+
3649
show()
3750

lib/matplotlib/axes.py

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5832,39 +5832,28 @@ def fill_between(self, x, y1, y2=0, where=None, **kwargs):
58325832
self._process_unit_info(xdata=x, ydata=y1, kwargs=kwargs)
58335833
self._process_unit_info(ydata=y2)
58345834

5835-
if where is None:
5836-
where = np.ones(len(x), np.bool)
5837-
else:
5838-
where = np.asarray(where)
5839-
5840-
maskedx = isinstance(x, np.ma.MaskedArray)
5841-
maskedy1 = isinstance(y1, np.ma.MaskedArray)
5842-
maskedy2 = isinstance(y2, np.ma.MaskedArray)
5843-
5844-
if (maskedx or maskedy1 or maskedy2):
5845-
if maskedx:
5846-
where = where & (~x.mask)
5847-
5848-
if maskedy1:
5849-
where = where & (~y1.mask)
5850-
5851-
if maskedy2:
5852-
where = where & (~y2.mask)
5853-
5854-
58555835
# Convert the arrays so we can work with them
5856-
x = np.asarray(self.convert_xunits(x))
5857-
y1 = np.asarray(self.convert_yunits(y1))
5858-
y2 = np.asarray(self.convert_yunits(y2))
5836+
x = np.asanyarray(self.convert_xunits(x))
5837+
y1 = np.asanyarray(self.convert_yunits(y1))
5838+
y2 = np.asanyarray(self.convert_yunits(y2))
58595839

5860-
if not cbook.iterable(y1):
5840+
if y1.ndim == 0:
58615841
y1 = np.ones_like(x)*y1
5862-
5863-
if not cbook.iterable(y2):
5842+
if y2.ndim == 0:
58645843
y2 = np.ones_like(x)*y2
58655844

5845+
if where is None:
5846+
where = np.ones(len(x), np.bool)
5847+
else:
5848+
where = np.asarray(where, np.bool)
5849+
5850+
if not (x.shape == y1.shape == y2.shape == where.shape):
5851+
raise ValueError("Argument dimensions are incompatible")
58665852

5867-
assert( (len(x)==len(y1)) and (len(x)==len(y2)) and len(x)==len(where))
5853+
mask = reduce(ma.mask_or,
5854+
[ma.getmask(x), ma.getmask(y1), ma.getmask(y2)])
5855+
if mask is not ma.nomask:
5856+
where &= ~mask
58685857

58695858
polys = []
58705859
for ind0, ind1 in mlab.contiguous_regions(where):

0 commit comments

Comments
 (0)