Revision: 6432
http://matplotlib.svn.sourceforge.net/matplotlib/?rev=6432&view=rev
Author: jdh2358
Date: 2008-11-23 19:04:35 +0000 (Sun, 23 Nov 2008)
Log Message:
-----------
generalized fill between poly collection
Modified Paths:
--------------
trunk/matplotlib/examples/api/filled_masked_regions.py
trunk/matplotlib/lib/matplotlib/collections.py
Modified: trunk/matplotlib/examples/api/filled_masked_regions.py
===================================================================
--- trunk/matplotlib/examples/api/filled_masked_regions.py 2008-11-21
18:20:00 UTC (rev 6431)
+++ trunk/matplotlib/examples/api/filled_masked_regions.py 2008-11-23
19:04:35 UTC (rev 6432)
@@ -8,31 +8,36 @@
t = np.arange(0.0, 2, 0.01)
-s = np.sin(2*np.pi*t)
+s1 = np.sin(2*np.pi*t)
+s2 = 1.2*np.sin(4*np.pi*t)
fig = plt.figure()
ax = fig.add_subplot(111)
-ax.set_title('using fill_between_masked')
-ax.plot(t, s, '-')
+ax.set_title('using fill_between_where')
+ax.plot(t, s1, t, s2)
ax.axhline(0, color='black', lw=2)
-collection = collections.PolyCollection.fill_between_masked(t, s, s>=0,
yboundary=0, color='green', alpha=0.5)
+collection = collections.PolyCollection.fill_between_where(
+ t, s1, s2, s1>=s2, color='green', alpha=0.5)
ax.add_collection(collection)
-collection = collections.PolyCollection.fill_between_masked(t, s, s<=0,
yboundary=0, color='red', alpha=0.5)
+collection = collections.PolyCollection.fill_between_where(
+ t, s1, s2, s1<=s2, color='red', alpha=0.5)
ax.add_collection(collection)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_title('using span_masked')
-ax.plot(t, s, '-')
+ax.plot(t, s1, '-')
ax.axhline(0, color='black', lw=2)
-collection = collections.BrokenBarHCollection.span_masked(t, s>0, ymin=0,
ymax=1, facecolor='green', alpha=0.5)
+collection = collections.BrokenBarHCollection.span_masked(
+ t, s1>0, ymin=0, ymax=1, facecolor='green', alpha=0.5)
ax.add_collection(collection)
-collection = collections.BrokenBarHCollection.span_masked(t, s<0, ymin=-1,
ymax=0, facecolor='red', alpha=0.5)
+collection = collections.BrokenBarHCollection.span_masked(
+ t, s1<0, ymin=-1, ymax=0, facecolor='red', alpha=0.5)
ax.add_collection(collection)
Modified: trunk/matplotlib/lib/matplotlib/collections.py
===================================================================
--- trunk/matplotlib/lib/matplotlib/collections.py 2008-11-21 18:20:00 UTC
(rev 6431)
+++ trunk/matplotlib/lib/matplotlib/collections.py 2008-11-23 19:04:35 UTC
(rev 6432)
@@ -674,7 +674,7 @@
@staticmethod
- def fill_between_masked(x, y, mask, yboundary=0, **kwargs):
+ def fill_between_where(x, y1, y2, mask, **kwargs):
"""
Create a :class:`PolyCollection` filling the regions between *y*
and *yboundary7* where ``mask==True``
@@ -683,35 +683,50 @@
*x*
an N length np array of the x data
- *y*
- an N length np array of the y data
+ *y1*
+ an N length scalar or np array of the x data
+ *y2*
+ an N length scalar or np array of the x data
+
*mask*
an N length numpy boolean array
- *yboundary*
- a scalar to fill between *y* and the boundary
-
*kwargs*
keyword args passed on to the :class:`PolyCollection`
"""
+ if not cbook.iterable(y1):
+ y1 = np.ones_like(x)*y1
+
+ if not cbook.iterable(y2):
+ y2 = np.ones_like(x)*y2
+
+ assert( (len(x)==len(y1)) and (len(x)==len(y2)) )
+
polys = []
for ind0, ind1 in mlab.contiguous_regions(mask):
theseverts = []
xslice = x[ind0:ind1]
- yslice = y[ind0:ind1]
+ y1slice = y1[ind0:ind1]
+ y2slice = y2[ind0:ind1]
+
if not len(xslice):
continue
N = len(xslice)
X = np.zeros((2*N+2, 2), np.float)
- X[0] = xslice[0], yboundary
- X[N+1] = xslice[-1], yboundary
+
+ # the purpose of the next two lines is for when y2 is a
+ # scalar like 0 and we want the fill to go all the way
+ # down to 0 even if none of the y1 sample points do
+ X[0] = xslice[0], y2slice[0]
+ X[N+1] = xslice[-1], y2slice[-1]
+
X[1:N+1,0] = xslice
- X[1:N+1,1] = yslice
+ X[1:N+1,1] = y1slice
X[N+2:,0] = xslice[::-1]
- X[N+2:,1] = yboundary
+ X[N+2:,1] = y2slice[::-1]
polys.append(X)
This was sent by the SourceForge.net collaborative development platform, the
world's largest Open Source development site.
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Matplotlib-checkins mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/matplotlib-checkins