Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bbb4197

Browse files
committed
generalized fill between poly collection
svn path=/trunk/matplotlib/; revision=6432
1 parent d9ad6a1 commit bbb4197

2 files changed

Lines changed: 39 additions & 19 deletions

File tree

examples/api/filled_masked_regions.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,36 @@
88

99

1010
t = np.arange(0.0, 2, 0.01)
11-
s = np.sin(2*np.pi*t)
11+
s1 = np.sin(2*np.pi*t)
12+
s2 = 1.2*np.sin(4*np.pi*t)
1213

1314
fig = plt.figure()
1415
ax = fig.add_subplot(111)
15-
ax.set_title('using fill_between_masked')
16-
ax.plot(t, s, '-')
16+
ax.set_title('using fill_between_where')
17+
ax.plot(t, s1, t, s2)
1718
ax.axhline(0, color='black', lw=2)
1819

19-
collection = collections.PolyCollection.fill_between_masked(t, s, s>=0, yboundary=0, color='green', alpha=0.5)
20+
collection = collections.PolyCollection.fill_between_where(
21+
t, s1, s2, s1>=s2, color='green', alpha=0.5)
2022
ax.add_collection(collection)
2123

22-
collection = collections.PolyCollection.fill_between_masked(t, s, s<=0, yboundary=0, color='red', alpha=0.5)
24+
collection = collections.PolyCollection.fill_between_where(
25+
t, s1, s2, s1<=s2, color='red', alpha=0.5)
2326
ax.add_collection(collection)
2427

2528

2629
fig = plt.figure()
2730
ax = fig.add_subplot(111)
2831
ax.set_title('using span_masked')
29-
ax.plot(t, s, '-')
32+
ax.plot(t, s1, '-')
3033
ax.axhline(0, color='black', lw=2)
3134

32-
collection = collections.BrokenBarHCollection.span_masked(t, s>0, ymin=0, ymax=1, facecolor='green', alpha=0.5)
35+
collection = collections.BrokenBarHCollection.span_masked(
36+
t, s1>0, ymin=0, ymax=1, facecolor='green', alpha=0.5)
3337
ax.add_collection(collection)
3438

35-
collection = collections.BrokenBarHCollection.span_masked(t, s<0, ymin=-1, ymax=0, facecolor='red', alpha=0.5)
39+
collection = collections.BrokenBarHCollection.span_masked(
40+
t, s1<0, ymin=-1, ymax=0, facecolor='red', alpha=0.5)
3641
ax.add_collection(collection)
3742

3843

lib/matplotlib/collections.py

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ def draw(self, renderer):
674674

675675

676676
@staticmethod
677-
def fill_between_masked(x, y, mask, yboundary=0, **kwargs):
677+
def fill_between_where(x, y1, y2, mask, **kwargs):
678678
"""
679679
Create a :class:`PolyCollection` filling the regions between *y*
680680
and *yboundary7* where ``mask==True``
@@ -683,35 +683,50 @@ def fill_between_masked(x, y, mask, yboundary=0, **kwargs):
683683
*x*
684684
an N length np array of the x data
685685
686-
*y*
687-
an N length np array of the y data
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
688691
689692
*mask*
690693
an N length numpy boolean array
691694
692-
*yboundary*
693-
a scalar to fill between *y* and the boundary
694-
695695
*kwargs*
696696
keyword args passed on to the :class:`PolyCollection`
697697
698698
"""
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+
699707
polys = []
700708
for ind0, ind1 in mlab.contiguous_regions(mask):
701709
theseverts = []
702710
xslice = x[ind0:ind1]
703-
yslice = y[ind0:ind1]
711+
y1slice = y1[ind0:ind1]
712+
y2slice = y2[ind0:ind1]
713+
704714
if not len(xslice):
705715
continue
706716

707717
N = len(xslice)
708718
X = np.zeros((2*N+2, 2), np.float)
709-
X[0] = xslice[0], yboundary
710-
X[N+1] = xslice[-1], yboundary
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+
711726
X[1:N+1,0] = xslice
712-
X[1:N+1,1] = yslice
727+
X[1:N+1,1] = y1slice
713728
X[N+2:,0] = xslice[::-1]
714-
X[N+2:,1] = yboundary
729+
X[N+2:,1] = y2slice[::-1]
715730

716731
polys.append(X)
717732

0 commit comments

Comments
 (0)