Skip to content

Commit 391198e

Browse files
committed
added interpolate functionaly to fill_betweenx, as in fill_between, enabled through keyword argument interpolate.
1 parent cb00f0b commit 391198e

File tree

2 files changed

+45
-8
lines changed

2 files changed

+45
-8
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: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4810,13 +4810,14 @@ def get_interp_point(ind):
48104810
label_namer=None)
48114811
@docstring.dedent_interpd
48124812
def fill_betweenx(self, y, x1, x2=0, where=None,
4813-
step=None, **kwargs):
4813+
step=None, interpolate=False, **kwargs):
48144814
"""
48154815
Make filled polygons between two horizontal curves.
48164816
48174817
Call signature::
48184818
4819-
fill_betweenx(y, x1, x2=0, where=None, **kwargs)
4819+
fill_betweenx(y, x1, x2=0, where=None, step=None,
4820+
interpolate=False, **kwargs)
48204821
48214822
Create a :class:`~matplotlib.collections.PolyCollection`
48224823
filling the regions between *x1* and *x2* where
@@ -4841,6 +4842,12 @@ def fill_betweenx(self, y, x1, x2=0, where=None,
48414842
step : {'pre', 'post', 'mid'}, optional
48424843
If not None, fill with step logic.
48434844
4845+
interpolate : bool, optional
4846+
If `True`, interpolate between the two lines to find the
4847+
precise point of intersection. Otherwise, the start and
4848+
end points of the filled region will only occur on explicit
4849+
values in the *x* array.
4850+
48444851
Notes
48454852
-----
48464853
@@ -4902,13 +4909,37 @@ def fill_betweenx(self, y, x1, x2=0, where=None,
49024909
continue
49034910

49044911
N = len(yslice)
4905-
Y = np.zeros((2 * N + 2, 2), float)
4912+
Y = np.zeros((2 * N + 2, 2), np.float)
4913+
if interpolate:
4914+
def get_interp_point(ind):
4915+
im1 = max(ind - 1, 0)
4916+
y_values = y[im1:ind + 1]
4917+
diff_values = x1[im1:ind + 1] - x2[im1:ind + 1]
4918+
x1_values = x1[im1:ind + 1]
4919+
4920+
if len(diff_values) == 2:
4921+
if np.ma.is_masked(diff_values[1]):
4922+
return x1[im1], y[im1]
4923+
elif np.ma.is_masked(diff_values[0]):
4924+
return x1[ind], y[ind]
4925+
4926+
diff_order = diff_values.argsort()
4927+
diff_root_y = np.interp(
4928+
0, diff_values[diff_order], y_values[diff_order])
4929+
diff_root_x = np.interp(diff_root_y, y_values, x1_values)
4930+
return diff_root_x, diff_root_y
4931+
4932+
start = get_interp_point(ind0)
4933+
end = get_interp_point(ind1)
4934+
else:
4935+
# the purpose of the next two lines is for when x2 is a
4936+
# scalar like 0 and we want the fill to go all the way
4937+
# down to 0 even if none of the x1 sample points do
4938+
start = x2slice[0], yslice[0]
4939+
end = x2slice[-1], yslice[-1]
49064940

4907-
# the purpose of the next two lines is for when x2 is a
4908-
# scalar like 0 and we want the fill to go all the way
4909-
# down to 0 even if none of the x1 sample points do
4910-
Y[0] = x2slice[0], yslice[0]
4911-
Y[N + 1] = x2slice[-1], yslice[-1]
4941+
Y[0] = start
4942+
Y[N + 1] = end
49124943

49134944
Y[1:N + 1, 0] = x1slice
49144945
Y[1:N + 1, 1] = yslice

0 commit comments

Comments
 (0)