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

Skip to content

Commit 2db768e

Browse files
committed
added poly_netween and updated fill_between
svn path=/trunk/matplotlib/; revision=3527
1 parent c2d44e9 commit 2db768e

5 files changed

Lines changed: 89 additions & 10 deletions

File tree

examples/fill_between.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,29 @@
11
#!/usr/bin/env python
2+
import matplotlib.mlab as mlab
3+
from pylab import figure, show
4+
import numpy as npy
25

3-
from pylab import *
6+
x = npy.arange(0, 2, 0.01)
7+
y1 = npy.sin(2*npy.pi*x)
8+
y2 = npy.sin(4*npy.pi*x) + 2
49

5-
x1 = arange(0, 2, 0.01)
6-
y1 = sin(2*pi*x1)
7-
y2 = sin(4*pi*x1) + 2
10+
fig = figure()
11+
ax = fig.add_subplot(311)
12+
ax2 = fig.add_subplot(312)
13+
ax3 = fig.add_subplot(313)
814

9-
# reverse x and y2 so the polygon fills in order
10-
x = concatenate( (x1,x1[::-1]) )
11-
y = concatenate( (y1,y2[::-1]) )
1215

13-
p = fill(x, y, facecolor='g', alpha=0.5)
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)
22+
ax2.set_ylabel('between y1 and 1')
23+
24+
xs, ys = mlab.poly_between(x, y1, y2)
25+
ax3.fill(xs, ys)
26+
ax3.set_ylabel('between y1 and y2')
27+
ax3.set_xlabel('x')
1428
show()
1529

lib/matplotlib/axes.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4236,6 +4236,14 @@ def fill(self, *args, **kwargs):
42364236
The same color strings that plot supports are supported by the fill
42374237
format string.
42384238
4239+
If you would like to fill below a curve, eg shade a region
4240+
between 0 and y along x, use mlab.poly_between, eg
4241+
4242+
xs, ys = poly_between(x, 0, y)
4243+
axes.fill(xs, ys, facecolor='red', alpha=0.5)
4244+
4245+
See examples/fill_between.py for more examples.
4246+
42394247
kwargs control the Polygon properties:
42404248
%(Polygon)s
42414249
"""

lib/matplotlib/cbook.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,19 @@ def enumerate(seq):
472472
yield i, seq[i]
473473

474474

475+
# use reversed builtin if available, else use python version
476+
try:
477+
import __builtin__
478+
reversed = __builtin__.reversed
479+
except:
480+
def reversed(seq):
481+
"""Python equivalent to the enumerate builtin function
482+
enumerate() is new in Python 2.3
483+
"""
484+
for i in range(len(seq)-1,-1,-1):
485+
yield i, seq[i]
486+
487+
475488
# use itertools.izip if available, else use python version
476489
try:
477490
import itertools

lib/matplotlib/mlab.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
from __future__ import division
5858
import sys, random, datetime, csv
5959

60+
import numpy as npy
6061

6162
from matplotlib import verbose
6263
import numpy as npy
@@ -77,7 +78,7 @@
7778
from numerix.mlab import hanning, cov, diff, svd, rand, std
7879
from numerix.fft import fft, inverse_fft
7980

80-
from cbook import iterable, is_string_like, to_filehandle
81+
from cbook import iterable, is_string_like, to_filehandle, reversed
8182

8283
try: set
8384
except NameError:
@@ -1680,6 +1681,7 @@ def angle(x1, y1, x2, y2):
16801681
angles += a
16811682
return nx.nonzero(nx.greater_equal(nx.absolute(angles), nx.pi))
16821683

1684+
16831685
def inside_poly(points, verts):
16841686
"""
16851687
points is a sequence of x,y points
@@ -1690,6 +1692,48 @@ def inside_poly(points, verts):
16901692
"""
16911693
return nx.nonzero(nxutils.points_inside_poly(points, verts))
16921694

1695+
def poly_below(xmin, xs, ys):
1696+
"""
1697+
given a sequence of xs and ys, return the vertices of a polygon
1698+
that has a horzontal base at xmin and an upper bound at the ys.
1699+
xmin is a scalar.
1700+
1701+
intended for use with Axes.fill, eg
1702+
xv, yv = poly_below(0, x, y)
1703+
ax.fill(xv, yv)
1704+
"""
1705+
xs = npy.asarray(xs)
1706+
ys = npy.asarray(ys)
1707+
Nx = len(xs)
1708+
Ny = len(ys)
1709+
assert(Nx==Ny)
1710+
x = xmin*npy.ones(2*Nx)
1711+
y = npy.ones(2*Nx)
1712+
x[:Nx] = xs
1713+
y[:Nx] = ys
1714+
y[Nx:] = ys[::-1]
1715+
return x, y
1716+
1717+
1718+
def poly_between(x, ylower, yupper):
1719+
"""
1720+
given a sequence of x, ylower and yupper, return the polygon that
1721+
fills the regions between them. ylower or yupper can be scalar or
1722+
iterable. If they are iterable, they must be equal in length to x
1723+
1724+
return value is x, y arrays for use with Axes.fill
1725+
"""
1726+
Nx = len(x)
1727+
if not iterable(ylower):
1728+
ylower = ylower*npy.ones(Nx)
1729+
1730+
if not iterable(yupper):
1731+
yupper = yupper*npy.ones(Nx)
1732+
1733+
x = npy.concatenate( (x, x[::-1]) )
1734+
y = npy.concatenate( (yupper, ylower[::-1]) )
1735+
return x,y
1736+
16931737
### the following code was written and submitted by Fernando Perez
16941738
### from the ipython numutils package under a BSD license
16951739
# begin fperez functions

lib/matplotlib/pylab.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
This is a matlab(TM) style interface to matplotlib.
2+
This is a procedural interface to matplotlib.
33
44
The following plotting commands are provided; some of these do not
55
exist in matlab(TM) but have proven themselves to be useful nonetheless.

0 commit comments

Comments
 (0)