@@ -4853,10 +4853,11 @@ def get_interp_point(ind):
4853
4853
label_namer = None )
4854
4854
@docstring .dedent_interpd
4855
4855
def fill_betweenx (self , y , x1 , x2 = 0 , where = None ,
4856
- step = None , ** kwargs ):
4856
+ step = None , interpolate = False , ** kwargs ):
4857
4857
"""
4858
4858
Make filled polygons between two horizontal curves.
4859
4859
4860
+
4860
4861
Create a :class:`~matplotlib.collections.PolyCollection`
4861
4862
filling the regions between *x1* and *x2* where
4862
4863
``where==True``
@@ -4880,6 +4881,12 @@ def fill_betweenx(self, y, x1, x2=0, where=None,
4880
4881
step : {'pre', 'post', 'mid'}, optional
4881
4882
If not None, fill with step logic.
4882
4883
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
+
4883
4890
Notes
4884
4891
-----
4885
4892
@@ -4948,13 +4955,37 @@ def fill_betweenx(self, y, x1, x2=0, where=None,
4948
4955
continue
4949
4956
4950
4957
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 ]
4952
4986
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
4958
4989
4959
4990
Y [1 :N + 1 , 0 ] = x1slice
4960
4991
Y [1 :N + 1 , 1 ] = yslice
0 commit comments