Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 11aee86

Browse files
khchansolvents
authored andcommitted
Renamed vp_coverage baseline images, added bw_method paramater to violinplot and updated demo
1 parent ca7d605 commit 11aee86

33 files changed

+31
-24
lines changed

examples/statistics/violinplot_demo.py

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,42 +11,33 @@
1111
pos = [1,2,4,5,7,8]
1212
data = [np.random.normal(size=100) for i in pos]
1313

14-
# TODO: future customizability dicts go here
15-
16-
# (From boxplot demo)
17-
# demonstrate how to customize the display different elements:
18-
# boxprops = dict(linestyle='--', linewidth=3, color='darkgoldenrod')
19-
# flierprops = dict(marker='o', markerfacecolor='green', markersize=12,
20-
# linestyle='none')
21-
# medianprops = dict(linestyle='-.', linewidth=2.5, color='firebrick')
22-
# meanpointprops = dict(marker='D', markeredgecolor='black',
23-
# markerfacecolor='firebrick')
24-
# meanlineprops = dict(linestyle='--', linewidth=2.5, color='purple')
25-
2614
fig, axes = plt.subplots(nrows=2, ncols=3, figsize=(6,6))
2715

28-
axes[0, 0].violinplot(data, pos, points=20, widths=0.1, showmeans=True,
29-
showextrema=True, showmedians=True)
16+
axes[0, 0].violinplot(data, pos, points=20, widths=0.1,
17+
showmeans=True, showextrema=True, showmedians=True)
3018
axes[0, 0].set_title('Custom violinplot 1', fontsize=fs)
3119

32-
axes[0, 1].violinplot(data, pos, points=40, widths=0.3, showmeans=True,
33-
showextrema=True, showmedians=True)
20+
axes[0, 1].violinplot(data, pos, points=40, widths=0.3,
21+
showmeans=True, showextrema=True, showmedians=True,
22+
bw_method='silverman')
3423
axes[0, 1].set_title('Custom violinplot 2', fontsize=fs)
3524

3625
axes[0, 2].violinplot(data, pos, points=60, widths=0.5, showmeans=True,
37-
showextrema=True, showmedians=True)
26+
showextrema=True, showmedians=True, bw_method=0.5)
3827
axes[0, 2].set_title('Custom violinplot 3', fontsize=fs)
3928

4029
axes[1, 0].violinplot(data, pos, points=80, vert=False, widths=0.7,
4130
showmeans=True, showextrema=True, showmedians=True)
4231
axes[1, 0].set_title('Custom violinplot 4', fontsize=fs)
4332

4433
axes[1, 1].violinplot(data, pos, points=100, vert=False, widths=0.9,
45-
showmeans=True, showextrema=True, showmedians=True)
34+
showmeans=True, showextrema=True, showmedians=True,
35+
bw_method='silverman')
4636
axes[1, 1].set_title('Custom violinplot 5', fontsize=fs)
4737

4838
axes[1, 2].violinplot(data, pos, points=200, vert=False, widths=1.1,
49-
showmeans=True, showextrema=True, showmedians=True)
39+
showmeans=True, showextrema=True, showmedians=True,
40+
bw_method=0.5)
5041
axes[1, 2].set_title('Custom violinplot 6', fontsize=fs)
5142

5243
for ax in axes.flatten():

lib/matplotlib/axes/_axes.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6727,7 +6727,7 @@ def matshow(self, Z, **kwargs):
67276727

67286728
def violinplot(self, dataset, positions=None, vert=True, widths=0.5,
67296729
showmeans=False, showextrema=True, showmedians=False,
6730-
points=100):
6730+
points=100, bw_method=None):
67316731
"""
67326732
Make a violin plot.
67336733
@@ -6774,6 +6774,13 @@ def violinplot(self, dataset, positions=None, vert=True, widths=0.5,
67746774
Defines the number of points to evaluate each of the gaussian
67756775
kernel density estimations at.
67766776
6777+
bw_method : str, scalar or callable, optional
6778+
The method used to calculate the estimator bandwidth. This can be
6779+
'scott', 'silverman', a scalar constant or a callable. If a
6780+
scalar, this will be used directly as `kde.factor`. If a
6781+
callable, it should take a `GaussianKDE` instance as only
6782+
parameter and return a scalar. If None (default), 'scott' is used.
6783+
67776784
Returns
67786785
-------
67796786
@@ -6852,7 +6859,7 @@ def violinplot(self, dataset, positions=None, vert=True, widths=0.5,
68526859
# Render violins
68536860
for data, pos, width in zip(dataset, positions, widths):
68546861
# Calculate the kernel density
6855-
kde = mlab.GaussianKDE(data)
6862+
kde = mlab.GaussianKDE(data, bw_method)
68566863
min_val = kde.dataset.min()
68576864
max_val = kde.dataset.max()
68586865
mean = np.mean(kde.dataset)

lib/matplotlib/mlab.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3669,32 +3669,41 @@ class GaussianKDE(object):
36693669
dataset : array_like
36703670
Datapoints to estimate from. In case of univariate data this is a 1-D
36713671
array, otherwise a 2-D array with shape (# of dims, # of data).
3672-
bw_method : str or scalar, optional
3672+
3673+
bw_method : str, scalar or callable, optional
36733674
The method used to calculate the estimator bandwidth. This can be
3674-
'scott', 'silverman', or a scalar constant. If a scalar, this will
3675-
be used directly as `kde.factor`. If None (default), 'scott' is used.
3675+
'scott', 'silverman', a scalar constant or a callable. If a
3676+
scalar, this will be used directly as `kde.factor`. If a
3677+
callable, it should take a `GaussianKDE` instance as only
3678+
parameter and return a scalar. If None (default), 'scott' is used.
36763679
36773680
Attributes
36783681
----------
36793682
dataset : ndarray
36803683
The dataset with which `gaussian_kde` was initialized.
3684+
36813685
dim : int
36823686
Number of dimensions.
3687+
36833688
num_dp : int
36843689
Number of datapoints.
3690+
36853691
factor : float
36863692
The bandwidth factor, obtained from `kde.covariance_factor`, with which
36873693
the covariance matrix is multiplied.
3694+
36883695
covariance : ndarray
36893696
The covariance matrix of `dataset`, scaled by the calculated bandwidth
36903697
(`kde.factor`).
3698+
36913699
inv_cov : ndarray
36923700
The inverse of `covariance`.
36933701
36943702
Methods
36953703
-------
36963704
kde.evaluate(points) : ndarray
36973705
Evaluate the estimated pdf on a provided set of points.
3706+
36983707
kde(points) : ndarray
36993708
Same as kde.evaluate(points)
37003709

0 commit comments

Comments
 (0)