@@ -15,6 +15,7 @@ def plot_kde(
15
15
rug = False ,
16
16
label = None ,
17
17
bw = 4.5 ,
18
+ quantiles = None ,
18
19
rotated = False ,
19
20
contour = True ,
20
21
fill_last = True ,
@@ -44,6 +45,9 @@ def plot_kde(
44
45
Bandwidth scaling factor for 1D KDE. Should be larger than 0. The higher this number the
45
46
smoother the KDE will be. Defaults to 4.5 which is essentially the same as the Scott's
46
47
rule of thumb (the default rule used by SciPy).
48
+ quantiles : list
49
+ Quantiles in ascending order used to segment the KDE. Use [.25, .5, .75] for quartiles.
50
+ Defaults to None.
47
51
rotated : bool
48
52
Whether to rotate the 1D KDE plot 90 degrees.
49
53
contour : bool
@@ -137,7 +141,6 @@ def plot_kde(
137
141
if fill_kwargs is None :
138
142
fill_kwargs = {}
139
143
140
- fill_kwargs .setdefault ("alpha" , 0 )
141
144
fill_kwargs .setdefault ("color" , default_color )
142
145
143
146
if rug_kwargs is None :
@@ -155,6 +158,11 @@ def plot_kde(
155
158
rug_space = max (density ) * rug_kwargs .pop ("space" )
156
159
157
160
x = np .linspace (lower , upper , len (density ))
161
+
162
+ if cumulative :
163
+ density_q = density
164
+ else :
165
+ density_q = np .cumsum (density )
158
166
fill_func = ax .fill_between
159
167
fill_x , fill_y = x , density
160
168
if rotated :
@@ -163,7 +171,6 @@ def plot_kde(
163
171
164
172
ax .tick_params (labelsize = xt_labelsize )
165
173
166
- ax .plot (x , density , label = label , ** plot_kwargs )
167
174
if rotated :
168
175
ax .set_xlim (0 , auto = True )
169
176
rug_x , rug_y = np .zeros_like (values ) - rug_space , values
@@ -173,7 +180,23 @@ def plot_kde(
173
180
174
181
if rug :
175
182
ax .plot (rug_x , rug_y , ** rug_kwargs )
176
- fill_func (fill_x , fill_y , ** fill_kwargs )
183
+
184
+ if quantiles is not None :
185
+ fill_kwargs .setdefault ("alpha" , 0.75 )
186
+
187
+ idx = [np .sum (density_q < quant ) for quant in quantiles ]
188
+
189
+ fill_func (
190
+ fill_x ,
191
+ fill_y ,
192
+ where = np .isin (fill_x , fill_x [idx ], invert = True , assume_unique = True ),
193
+ ** fill_kwargs
194
+ )
195
+ else :
196
+ fill_kwargs .setdefault ("alpha" , 0 )
197
+ ax .plot (x , density , label = label , ** plot_kwargs )
198
+ fill_func (fill_x , fill_y , ** fill_kwargs )
199
+
177
200
if label :
178
201
ax .legend ()
179
202
else :
@@ -233,7 +256,6 @@ def _fast_kde(x, cumulative=False, bw=4.5):
233
256
234
257
n_bins = min (int (len_x ** (1 / 3 ) * std_x * 2 ), 200 )
235
258
grid , _ = np .histogram (x , bins = n_bins )
236
- d_x = (xmax - xmin ) / (n_bins - 1 )
237
259
238
260
scotts_factor = len_x ** (- 0.2 )
239
261
kern_nx = int (scotts_factor * 2 * np .pi * std_x )
@@ -243,13 +265,10 @@ def _fast_kde(x, cumulative=False, bw=4.5):
243
265
grid = np .concatenate ([grid [npad :0 :- 1 ], grid , grid [n_bins : n_bins - npad : - 1 ]])
244
266
density = convolve (grid , kernel , mode = "same" )[npad : npad + n_bins ]
245
267
246
- norm_factor = len_x * d_x * (2 * np .pi * std_x ** 2 * scotts_factor ** 2 ) ** 0.5
247
-
248
- density = density / norm_factor
268
+ density /= np .sum (density )
249
269
250
270
if cumulative :
251
- cs_density = np .cumsum (density )
252
- density = cs_density / cs_density [- 1 ]
271
+ density = np .cumsum (density )
253
272
254
273
return density , xmin , xmax
255
274
0 commit comments