Skip to content

Commit 5370f28

Browse files
authored
Merge pull request matplotlib#30616 from rcomer/doc-violin_stats
DOC: add what's new info for violin_stats
2 parents ee6c7f6 + df150b2 commit 5370f28

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
``violin_stats`` simpler *method* parameter
2+
-------------------------------------------
3+
4+
The *method* parameter of `~.cbook.violin_stats` may now be specified as tuple of
5+
strings, and has a new default ``("GaussianKDE", "scott")``. Calling
6+
`~.cbook.violin_stats` followed by `~.Axes.violin` is therefore now equivalent to
7+
calling `~.Axes.violinplot`.
8+
9+
.. plot::
10+
:include-source: true
11+
:alt: Example showing violin_stats followed by violin gives the same result as violinplot
12+
13+
import matplotlib.pyplot as plt
14+
from matplotlib.cbook import violin_stats
15+
import numpy as np
16+
17+
rng = np.random.default_rng(19680801)
18+
data = rng.normal(size=(10, 3))
19+
20+
fig, (ax1, ax2) = plt.subplots(ncols=2, layout='constrained', figsize=(6.4, 3.5))
21+
22+
# Create the violin plot in one step
23+
ax1.violinplot(data)
24+
ax1.set_title('One Step')
25+
26+
# Process the data and then create the violin plot
27+
vstats = violin_stats(data)
28+
ax2.violin(vstats)
29+
ax2.set_title('Two Steps')
30+
31+
plt.show()

lib/matplotlib/axes/_axes.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9006,6 +9006,8 @@ def violin(self, vpstats, positions=None, vert=None,
90069006
--------
90079007
violinplot :
90089008
Draw a violin plot from data instead of pre-computed statistics.
9009+
.cbook.violin_stats:
9010+
Calculate a *vpstats* dictionary from data, suitable for passing to violin.
90099011
"""
90109012

90119013
# Statistical quantities to be plotted on the violins

lib/matplotlib/cbook.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1443,7 +1443,7 @@ def violin_stats(X, method=("GaussianKDE", "scott"), points=100, quantiles=None)
14431443
dictionary.
14441444
14451445
Users can skip this function and pass a user-defined set of dictionaries
1446-
with the same keys to `~.axes.Axes.violinplot` instead of using Matplotlib
1446+
with the same keys to `~.axes.Axes.violin` instead of using Matplotlib
14471447
to do the calculations. See the *Returns* section below for the keys
14481448
that must be present in the dictionaries.
14491449
@@ -1471,6 +1471,9 @@ def method(data: ndarray, coords: ndarray) -> ndarray
14711471
14721472
It should return the KDE of *data* evaluated at *coords*.
14731473
1474+
.. versionadded:: 3.11
1475+
Support for ``(name, bw_method)`` tuple.
1476+
14741477
points : int, default: 100
14751478
Defines the number of points to evaluate each of the gaussian kernel
14761479
density estimates at.

0 commit comments

Comments
 (0)