@@ -4646,6 +4646,11 @@ def reduce_C_function(C: array) -> float
4646
4646
# Count the number of data in each hexagon
4647
4647
x = np .array (x , float )
4648
4648
y = np .array (y , float )
4649
+
4650
+ if marginals :
4651
+ xorig = x .copy ()
4652
+ yorig = y .copy ()
4653
+
4649
4654
if xscale == 'log' :
4650
4655
if np .any (x <= 0.0 ):
4651
4656
raise ValueError ("x contains non-positive values, so can not"
@@ -4674,10 +4679,6 @@ def reduce_C_function(C: array) -> float
4674
4679
sx = (xmax - xmin ) / nx
4675
4680
sy = (ymax - ymin ) / ny
4676
4681
4677
- if marginals :
4678
- xorig = x .copy ()
4679
- yorig = y .copy ()
4680
-
4681
4682
x = (x - xmin ) / sx
4682
4683
y = (y - ymin ) / sy
4683
4684
ix1 = np .round (x ).astype (int )
@@ -4833,40 +4834,42 @@ def reduce_C_function(C: array) -> float
4833
4834
if not marginals :
4834
4835
return collection
4835
4836
4837
+ # Process marginals
4836
4838
if C is None :
4837
4839
C = np .ones (len (x ))
4838
4840
4839
- def coarse_bin (x , y , coarse ):
4840
- ind = coarse .searchsorted (x ).clip (0 , len (coarse ) - 1 )
4841
- mus = np .zeros (len (coarse ))
4842
- for i in range (len (coarse )):
4843
- yi = y [ind == i ]
4841
+ def coarse_bin (x , y , bin_edges ):
4842
+ """
4843
+ Sort x-values into bins defined by *bin_edges*, then for all the
4844
+ corresponding y-values in each bin use *reduce_c_function* to
4845
+ compute the bin value.
4846
+ """
4847
+ nbins = len (bin_edges ) - 1
4848
+ # Sort x-values into bins
4849
+ bin_idxs = np .searchsorted (bin_edges , x ) - 1
4850
+ mus = np .zeros (nbins ) * np .nan
4851
+ for i in range (nbins ):
4852
+ # Get y-values for each bin
4853
+ yi = y [bin_idxs == i ]
4844
4854
if len (yi ) > 0 :
4845
- mu = reduce_C_function (yi )
4846
- else :
4847
- mu = np .nan
4848
- mus [i ] = mu
4855
+ mus [i ] = reduce_C_function (yi )
4849
4856
return mus
4850
4857
4851
- coarse = np .linspace (xmin , xmax , gridsize )
4858
+ if xscale == 'log' :
4859
+ bin_edges = np .geomspace (xmin , xmax , nx + 1 )
4860
+ else :
4861
+ bin_edges = np .linspace (xmin , xmax , nx + 1 )
4862
+ xcoarse = coarse_bin (xorig , C , bin_edges )
4852
4863
4853
- xcoarse = coarse_bin (xorig , C , coarse )
4854
- valid = ~ np .isnan (xcoarse )
4855
4864
verts , values = [], []
4856
- for i , val in enumerate (xcoarse ):
4857
- thismin = coarse [i ]
4858
- if i < len (coarse ) - 1 :
4859
- thismax = coarse [i + 1 ]
4860
- else :
4861
- thismax = thismin + np .diff (coarse )[- 1 ]
4862
-
4863
- if not valid [i ]:
4865
+ for bin_left , bin_right , val in zip (
4866
+ bin_edges [:- 1 ], bin_edges [1 :], xcoarse ):
4867
+ if np .isnan (val ):
4864
4868
continue
4865
-
4866
- verts .append ([(thismin , 0 ),
4867
- (thismin , 0.05 ),
4868
- (thismax , 0.05 ),
4869
- (thismax , 0 )])
4869
+ verts .append ([(bin_left , 0 ),
4870
+ (bin_left , 0.05 ),
4871
+ (bin_right , 0.05 ),
4872
+ (bin_right , 0 )])
4870
4873
values .append (val )
4871
4874
4872
4875
values = np .array (values )
@@ -4881,20 +4884,21 @@ def coarse_bin(x, y, coarse):
4881
4884
hbar .update (kwargs )
4882
4885
self .add_collection (hbar , autolim = False )
4883
4886
4884
- coarse = np .linspace (ymin , ymax , gridsize )
4885
- ycoarse = coarse_bin (yorig , C , coarse )
4886
- valid = ~ np .isnan (ycoarse )
4887
+ if yscale == 'log' :
4888
+ bin_edges = np .geomspace (ymin , ymax , 2 * ny + 1 )
4889
+ else :
4890
+ bin_edges = np .linspace (ymin , ymax , 2 * ny + 1 )
4891
+ ycoarse = coarse_bin (yorig , C , bin_edges )
4892
+
4887
4893
verts , values = [], []
4888
- for i , val in enumerate (ycoarse ):
4889
- thismin = coarse [i ]
4890
- if i < len (coarse ) - 1 :
4891
- thismax = coarse [i + 1 ]
4892
- else :
4893
- thismax = thismin + np .diff (coarse )[- 1 ]
4894
- if not valid [i ]:
4894
+ for bin_bottom , bin_top , val in zip (
4895
+ bin_edges [:- 1 ], bin_edges [1 :], ycoarse ):
4896
+ if np .isnan (val ):
4895
4897
continue
4896
- verts .append ([(0 , thismin ), (0.0 , thismax ),
4897
- (0.05 , thismax ), (0.05 , thismin )])
4898
+ verts .append ([(0 , bin_bottom ),
4899
+ (0 , bin_top ),
4900
+ (0.05 , bin_top ),
4901
+ (0.05 , bin_bottom )])
4898
4902
values .append (val )
4899
4903
4900
4904
values = np .array (values )
0 commit comments