Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1ac9e56

Browse filesBrowse files
committed
added interpolate functionaly to fill_betweenx, as in fill_between, enabled through keyword argument interpolate.
1 parent 07adcde commit 1ac9e56
Copy full SHA for 1ac9e56

File tree

Expand file treeCollapse file tree

2 files changed

+48
-7
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+48
-7
lines changed
+6Lines 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

Copy file name to clipboardExpand all lines: lib/matplotlib/axes/_axes.py
+42-7Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4853,10 +4853,15 @@ 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+
Call signature::
4861+
4862+
fill_betweenx(y, x1, x2=0, where=None, step=None,
4863+
interpolate=False, **kwargs)
4864+
48604865
Create a :class:`~matplotlib.collections.PolyCollection`
48614866
filling the regions between *x1* and *x2* where
48624867
``where==True``
@@ -4880,6 +4885,12 @@ def fill_betweenx(self, y, x1, x2=0, where=None,
48804885
step : {'pre', 'post', 'mid'}, optional
48814886
If not None, fill with step logic.
48824887
4888+
interpolate : bool, optional
4889+
If `True`, interpolate between the two lines to find the
4890+
precise point of intersection. Otherwise, the start and
4891+
end points of the filled region will only occur on explicit
4892+
values in the *x* array.
4893+
48834894
Notes
48844895
-----
48854896
@@ -4948,13 +4959,37 @@ def fill_betweenx(self, y, x1, x2=0, where=None,
49484959
continue
49494960

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

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]
4991+
Y[0] = start
4992+
Y[N + 1] = end
49584993

49594994
Y[1:N + 1, 0] = x1slice
49604995
Y[1:N + 1, 1] = yslice

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.