@@ -4589,6 +4589,11 @@ def reduce_C_function(C: array) -> float
4589
4589
# Count the number of data in each hexagon
4590
4590
x = np .array (x , float )
4591
4591
y = np .array (y , float )
4592
+
4593
+ if marginals :
4594
+ xorig = x .copy ()
4595
+ yorig = y .copy ()
4596
+
4592
4597
if xscale == 'log' :
4593
4598
if np .any (x <= 0.0 ):
4594
4599
raise ValueError ("x contains non-positive values, so can not"
@@ -4617,10 +4622,6 @@ def reduce_C_function(C: array) -> float
4617
4622
sx = (xmax - xmin ) / nx
4618
4623
sy = (ymax - ymin ) / ny
4619
4624
4620
- if marginals :
4621
- xorig = x .copy ()
4622
- yorig = y .copy ()
4623
-
4624
4625
x = (x - xmin ) / sx
4625
4626
y = (y - ymin ) / sy
4626
4627
ix1 = np .round (x ).astype (int )
@@ -4746,11 +4747,6 @@ def reduce_C_function(C: array) -> float
4746
4747
vmin = vmax = None
4747
4748
bins = None
4748
4749
4749
- if isinstance (norm , mcolors .LogNorm ):
4750
- if (accum == 0 ).any ():
4751
- # make sure we have no zeros
4752
- accum += 1
4753
-
4754
4750
# autoscale the norm with current accum values if it hasn't
4755
4751
# been set
4756
4752
if norm is not None :
@@ -4781,40 +4777,42 @@ def reduce_C_function(C: array) -> float
4781
4777
if not marginals :
4782
4778
return collection
4783
4779
4780
+ # Process marginals
4784
4781
if C is None :
4785
4782
C = np .ones (len (x ))
4786
4783
4787
- def coarse_bin (x , y , coarse ):
4788
- ind = coarse .searchsorted (x ).clip (0 , len (coarse ) - 1 )
4789
- mus = np .zeros (len (coarse ))
4790
- for i in range (len (coarse )):
4791
- yi = y [ind == i ]
4784
+ def coarse_bin (x , y , bin_edges ):
4785
+ """
4786
+ Sort x-values into bins defined by *bin_edges*, then for all the
4787
+ corresponding y-values in each bin use *reduce_c_function* to
4788
+ compute the bin value.
4789
+ """
4790
+ nbins = len (bin_edges ) - 1
4791
+ # Sort x-values into bins
4792
+ bin_idxs = np .searchsorted (bin_edges , x ) - 1
4793
+ mus = np .zeros (nbins ) * np .nan
4794
+ for i in range (nbins ):
4795
+ # Get y-values for each bin
4796
+ yi = y [bin_idxs == i ]
4792
4797
if len (yi ) > 0 :
4793
- mu = reduce_C_function (yi )
4794
- else :
4795
- mu = np .nan
4796
- mus [i ] = mu
4798
+ mus [i ] = reduce_C_function (yi )
4797
4799
return mus
4798
4800
4799
- coarse = np .linspace (xmin , xmax , gridsize )
4801
+ if xscale == 'log' :
4802
+ bin_edges = np .geomspace (xmin , xmax , nx + 1 )
4803
+ else :
4804
+ bin_edges = np .linspace (xmin , xmax , nx + 1 )
4805
+ xcoarse = coarse_bin (xorig , C , bin_edges )
4800
4806
4801
- xcoarse = coarse_bin (xorig , C , coarse )
4802
- valid = ~ np .isnan (xcoarse )
4803
4807
verts , values = [], []
4804
- for i , val in enumerate (xcoarse ):
4805
- thismin = coarse [i ]
4806
- if i < len (coarse ) - 1 :
4807
- thismax = coarse [i + 1 ]
4808
- else :
4809
- thismax = thismin + np .diff (coarse )[- 1 ]
4810
-
4811
- if not valid [i ]:
4808
+ for bin_left , bin_right , val in zip (
4809
+ bin_edges [:- 1 ], bin_edges [1 :], xcoarse ):
4810
+ if np .isnan (val ):
4812
4811
continue
4813
-
4814
- verts .append ([(thismin , 0 ),
4815
- (thismin , 0.05 ),
4816
- (thismax , 0.05 ),
4817
- (thismax , 0 )])
4812
+ verts .append ([(bin_left , 0 ),
4813
+ (bin_left , 0.05 ),
4814
+ (bin_right , 0.05 ),
4815
+ (bin_right , 0 )])
4818
4816
values .append (val )
4819
4817
4820
4818
values = np .array (values )
@@ -4829,20 +4827,21 @@ def coarse_bin(x, y, coarse):
4829
4827
hbar .update (kwargs )
4830
4828
self .add_collection (hbar , autolim = False )
4831
4829
4832
- coarse = np .linspace (ymin , ymax , gridsize )
4833
- ycoarse = coarse_bin (yorig , C , coarse )
4834
- valid = ~ np .isnan (ycoarse )
4830
+ if yscale == 'log' :
4831
+ bin_edges = np .geomspace (ymin , ymax , 2 * ny + 1 )
4832
+ else :
4833
+ bin_edges = np .linspace (ymin , ymax , 2 * ny + 1 )
4834
+ ycoarse = coarse_bin (yorig , C , bin_edges )
4835
+
4835
4836
verts , values = [], []
4836
- for i , val in enumerate (ycoarse ):
4837
- thismin = coarse [i ]
4838
- if i < len (coarse ) - 1 :
4839
- thismax = coarse [i + 1 ]
4840
- else :
4841
- thismax = thismin + np .diff (coarse )[- 1 ]
4842
- if not valid [i ]:
4837
+ for bin_bottom , bin_top , val in zip (
4838
+ bin_edges [:- 1 ], bin_edges [1 :], ycoarse ):
4839
+ if np .isnan (val ):
4843
4840
continue
4844
- verts .append ([(0 , thismin ), (0.0 , thismax ),
4845
- (0.05 , thismax ), (0.05 , thismin )])
4841
+ verts .append ([(0 , bin_bottom ),
4842
+ (0 , bin_top ),
4843
+ (0.05 , bin_top ),
4844
+ (0.05 , bin_bottom )])
4846
4845
values .append (val )
4847
4846
4848
4847
values = np .array (values )
0 commit comments