8000 Merge pull request #6560 from rmlopes/fillbetween · matplotlib/matplotlib@3ff9fa8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3ff9fa8

Browse files
authored
Merge pull request #6560 from rmlopes/fillbetween
ENH Fillbetween now has an interpolate argument.
2 parents bdf2146 + 4e9c7e7 commit 3ff9fa8

File tree

2 files changed

+44
-7
lines changed

2 files changed

+44
-7
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Interpolation in fill_betweenx
2+
------------------------------
3+
4+
The ``interpolate`` parameter now exists for the method :func:`fill_betweenx`.
5+
This allows a user to interpolate the data and fill the areas in the crossover
6+
points, similarly to :func:`fill_between`.

lib/matplotlib/axes/_axes.py

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4853,10 +4853,11 @@ def get_interp_point(ind):
48534853
label_namer=None)
48544854
@docstring.dedent_interpd
48554855
def fill_betweenx(self, y, x1, x2=0, where=None,
4856-
step=None, **kwargs):
4856+
step=None, interpolate=False, **kwargs):
48574857
"""
48584858
Make filled polygons between two horizontal curves.
48594859
4860+
48604861
Create a :class:`~matplotlib.collections.PolyCollection`
48614862
filling the regions between *x1* and *x2* where
48624863
``where==True``
@@ -4880,6 +4881,12 @@ def fill_betweenx(self, y, x1, x2=0, where=None,
48804881
step : {'pre', 'post', 'mid'}, optional
48814882
If not None, fill with step logic.
48824883
4884+
interpolate : bool, optional
4885+
If `True`, interpolate between the two lines to find the
4886+
precise point of intersection. Otherwise, the start and
4887+
end points of the filled region will only occur on explicit
4888+
values in the *x* array.
4889+
48834890
Notes
48844891
-----
48854892
@@ -4948,13 +4955,37 @@ def fill_betweenx(self, y, x1, x2=0, where=None,
49484955
continue
49494956

49504957
N = len(yslice)
4951-
Y = np.zeros((2 * N + 2, 2), float)
4958+
Y = np.zeros((2 * N + 2, 2), np.float)
4959+
if interpolate:
4960+
def get_interp_point(ind):
4961+
im1 = max(ind - 1, 0)
4962+
y_values = y[im1:ind + 1]
4963+
diff_values = x1[im1:ind + 1] - x2[im1:ind + 1]
4964+
x1_values = x1[im1:ind + 1]
4965+
4966+
if len(diff_values) == 2:
4967+
if np.ma.is_masked(diff_values[1]):
4968+
return x1[im1], y[im1]
4969+
elif np.ma.is_masked(diff_values[0]):
4970+
return x1[ind], y[ind]
4971+
4972+
diff_order = diff_values.argsort()
4973+
diff_root_y = np.interp(
4974+
0, diff_values[diff_order], y_values[diff_order])
4975+
diff_root_x = np.interp(diff_root_y, y_values, x1_values)
4976+
return diff_root_x, diff_root_y
4977+
4978+
start = get_interp_point(ind0)
4979+
end = get_interp_point(ind1)
4980+
else:
4981+
# the purpose of the next two lines is for when x2 is a
4982+
# scalar like 0 and we want the fill to go all the way
4983+
# down to 0 even if none of the x1 sample points do
4984+
start = x2slice[0], yslice[0]
4985+
end = x2slice[-1], yslice[-1]
49524986

4953-
# the purpose of the next two lines is for when x2 is a
4954-
# scalar like 0 and we want the fill to go all the way
4955-
# down to 0 even if none of the x1 sample points do
4956-
Y[0] = x2slice[0], yslice[0]
4957-
Y[N + 1] = x2slice[-1], yslice[-1]
4987+
Y[0] = start
4988+
Y[N + 1] = end
49584989

49594990
Y[1:N + 1, 0] = x1slice
49604991
Y[1:N + 1, 1] = yslice

0 commit comments

Comments
 (0)
0