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

Skip to content

Commit a3912a9

Browse files
committed
Use np.quantile instead of percentile
1 parent 3d46d7f commit a3912a9

File tree

4 files changed

+19
-17
lines changed

4 files changed

+19
-17
lines changed

examples/statistics/violinplot.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,13 @@
4545

4646
axes[0, 3].violinplot(data, pos, points=60, widths=0.7, showmeans=True,
4747
showextrema=True, showmedians=True, bw_method=0.5,
48-
percentiles=[[10], [], [], [17.5, 95.4], [75], [25]])
48+
percentiles=[[0.1], [], [], [0.175, 0.954], [0.75],
49+
[0.25]])
4950
axes[0, 3].set_title('Custom violinplot 4', fontsize=fs)
5051

5152
axes[0, 4].violinplot(data[-1:], pos[-1:], points=60, widths=0.7,
5253
showmeans=True, showextrema=True, showmedians=True,
53-
percentiles=[5, 10, 80, 90], bw_method=0.5)
54+
percentiles=[0.05, 0.1, 0.8, 0.9], bw_method=0.5)
5455
axes[0, 4].set_title('Custom violinplot 5', fontsize=fs)
5556

5657
axes[1, 0].violinplot(data, pos, points=80, vert=False, widths=0.7,
@@ -69,13 +70,14 @@
6970

7071
axes[1, 3].violinplot(data, pos, points=200, vert=False, widths=1.1,
7172
showmeans=True, showextrema=True, showmedians=True,
72-
percentiles=[[10], [], [], [17.5, 95.4], [75], [25]],
73+
percentiles=[[0.1], [], [], [0.175, 0.954], [0.75],
74+
[0.25]],
7375
bw_method=0.5)
7476
axes[1, 3].set_title('Custom violinplot 9', fontsize=fs)
7577

7678
axes[1, 4].violinplot(data[-1:], pos[-1:], points=200, vert=False, widths=1.1,
7779
showmeans=True, showextrema=True, showmedians=True,
78-
percentiles=[5, 10, 80, 90], bw_method=0.5)
80+
percentiles=[0.05, 0.1, 0.8, 0.9], bw_method=0.5)
7981
axes[1, 4].set_title('Custom violinplot 10', fontsize=fs)
8082

8183
for ax in axes.flat:

lib/matplotlib/axes/_axes.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7917,9 +7917,9 @@ def violinplot(self, dataset, positions=None, vert=True, widths=0.5,
79177917
If `True`, will toggle rendering of the medians.
79187918
79197919
percentiles : array-like, default=None
7920-
If not None, set a list of floats in interval [0, 100] for each
7921-
violin, which stands for the percentiles that will be rendered for
7922-
that violin.
7920+
If not None, set a list of floats in interval [0, 1] for each violin,
7921+
which stands for the percentiles that will be rendered for that
7922+
violin.
79237923
79247924
points : scalar, default = 100
79257925
Defines the number of points to evaluate each of the

lib/matplotlib/cbook/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1456,10 +1456,10 @@ def violin_stats(X, method, points=100, percentiles=None):
14561456
density estimates at.
14571457
14581458
percentiles : array-like, default = None
1459-
Defines (if not None) a list of floats in interval [0, 100] for each
1459+
Defines (if not None) a list of floats in interval [0, 1] for each
14601460
column of data, which represents the percentiles that will be rendered
14611461
for that column of data. Must have 2 or fewer dimensions. 1D array will
1462-
be returned in a singleton list containing them.
1462+
be treated as a singleton list containing them.
14631463
14641464
Returns
14651465
-------
@@ -1506,7 +1506,7 @@ def violin_stats(X, method, points=100, percentiles=None):
15061506
# Calculate basic stats for the distribution
15071507
min_val = np.min(x)
15081508
max_val = np.max(x)
1509-
p_val = np.percentile(x, p)
1509+
p_val = np.quantile(x, p)
15101510

15111511
# Evaluate the kernel density estimate
15121512
coords = np.linspace(min_val, max_val, points)

lib/matplotlib/tests/test_axes.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2662,7 +2662,7 @@ def test_vert_violinplot_showall():
26622662
data = [np.random.normal(size=100) for i in range(4)]
26632663
ax.violinplot(data, positions=range(4), showmeans=1, showextrema=1,
26642664
showmedians=1,
2665-
percentiles=[[10, 90], [20, 80], [30, 70], [40, 60]])
2665+
percentiles=[[0.1, 0.9], [0.2, 0.8], [0.3, 0.7], [0.4, 0.6]])
26662666

26672667

26682668
@image_comparison(baseline_images=['violinplot_vert_custompoints_10'],
@@ -2740,7 +2740,7 @@ def test_horiz_violinplot_showall():
27402740
data = [np.random.normal(size=100) for i in range(4)]
27412741
ax.violinplot(data, positions=range(4), vert=False, showmeans=1,
27422742
showextrema=1, showmedians=1,
2743-
percentiles=[[10, 90], [20, 80], [30, 70], [40, 60]])
2743+
percentiles=[[0.1, 0.9], [0.2, 0.8], [0.3, 0.7], [0.4, 0.6]])
27442744

27452745

27462746
@image_comparison(baseline_images=['violinplot_horiz_custompoints_10'],
@@ -2791,7 +2791,7 @@ def test_violinplot_bad_percentiles():
27912791

27922792
# Different size percentile list and plots
27932793
with pytest.raises(ValueError):
2794-
ax.violinplot(data, percentiles=[[10, 20], [50]])
2794+
ax.violinplot(data, percentiles=[[0.1, 0.2], [0.5]])
27952795

27962796

27972797
def test_violinplot_outofrange_percentiles():
@@ -2802,11 +2802,11 @@ def test_violinplot_outofrange_percentiles():
28022802

28032803
# Percentile value above 100
28042804
with pytest.raises(ValueError):
2805-
ax.violinplot(data, percentiles=[[10, 20, 30, 105]])
2805+
ax.violinplot(data, percentiles=[[0.1, 0.2, 0.3, 1.05]])
28062806

28072807
# Percentile value below 0
28082808
with pytest.raises(ValueError):
2809-
ax.violinplot(data, percentiles=[[-5, 20, 30, 75]])
2809+
ax.violinplot(data, percentiles=[[-0.05, 0.2, 0.3, 0.75]])
28102810

28112811

28122812
@check_figures_equal(extensions=["png"])
@@ -2818,11 +2818,11 @@ def test_violinplot_single_list_percentiles(fig_test, fig_ref):
28182818

28192819
# Test image
28202820
ax = fig_test.subplots()
2821-
ax.violinplot(data, percentiles=[10, 30, 90])
2821+
ax.violinplot(data, percentiles=[0.1, 0.3, 0.9])
28222822

28232823
# Reference image
28242824
ax = fig_ref.subplots()
2825-
ax.violinplot(data, percentiles=[[10, 30, 90]])
2825+
ax.violinplot(data, percentiles=[[0.1, 0.3, 0.9]])
28262826

28272827

28282828
def test_manage_xticks():

0 commit comments

Comments
 (0)