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

Skip to content

Commit 63669d4

Browse files
committed
add violin orientation param
1 parent 78b6bdc commit 63669d4

File tree

7 files changed

+138
-24
lines changed

7 files changed

+138
-24
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
``violinplot`` and ``violin`` *vert* parameter
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
The parameter *vert: bool* has been deprecated on `~.Axes.violinplot` and
5+
`~.Axes.violin`.
6+
It will be replaced by *orientation: {"vertical", "horizontal"}* for API
7+
consistency.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
``violinplot`` and ``violin`` orientation parameter
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
Violinplots have a new parameter *orientation: {"vertical", "horizontal"}*
5+
to change the orientation of the plot. This will replace the deprecated
6+
*vert: bool* parameter.
7+
8+
9+
.. plot::
10+
:include-source: true
11+
:alt: Example of creating 4 horizontal violinplots.
12+
13+
import matplotlib.pyplot as plt
14+
import numpy as np
15+
16+
fig, ax = plt.subplots()
17+
np.random.seed(19680801)
18+
all_data = [np.random.normal(0, std, 100) for std in range(6, 10)]
19+
20+
ax.violinplot(all_data, orientation='horizontal')
21+
plt.show()

galleries/examples/statistics/violinplot.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,37 +62,37 @@
6262
quantiles=[0.05, 0.1, 0.8, 0.9], bw_method=0.5, side='high')
6363
axs[0, 5].set_title('Custom violin 6', fontsize=fs)
6464

65-
axs[1, 0].violinplot(data, pos, points=80, vert=False, widths=0.7,
65+
axs[1, 0].violinplot(data, pos, points=80, orientation='horizontal', widths=0.7,
6666
showmeans=True, showextrema=True, showmedians=True)
6767
axs[1, 0].set_title('Custom violin 7', fontsize=fs)
6868

69-
axs[1, 1].violinplot(data, pos, points=100, vert=False, widths=0.9,
69+
axs[1, 1].violinplot(data, pos, points=100, orientation='horizontal', widths=0.9,
7070
showmeans=True, showextrema=True, showmedians=True,
7171
bw_method='silverman')
7272
axs[1, 1].set_title('Custom violin 8', fontsize=fs)
7373

74-
axs[1, 2].violinplot(data, pos, points=200, vert=False, widths=1.1,
74+
axs[1, 2].violinplot(data, pos, points=200, orientation='horizontal', widths=1.1,
7575
showmeans=True, showextrema=True, showmedians=True,
7676
bw_method=0.5)
7777
axs[1, 2].set_title('Custom violin 9', fontsize=fs)
7878

79-
axs[1, 3].violinplot(data, pos, points=200, vert=False, widths=1.1,
79+
axs[1, 3].violinplot(data, pos, points=200, orientation='horizontal', widths=1.1,
8080
showmeans=True, showextrema=True, showmedians=True,
8181
quantiles=[[0.1], [], [], [0.175, 0.954], [0.75], [0.25]],
8282
bw_method=0.5)
8383
axs[1, 3].set_title('Custom violin 10', fontsize=fs)
8484

85-
axs[1, 4].violinplot(data[-1:], pos[-1:], points=200, vert=False, widths=1.1,
86-
showmeans=True, showextrema=True, showmedians=True,
85+
axs[1, 4].violinplot(data[-1:], pos[-1:], points=200, orientation='horizontal',
86+
widths=1.1, showmeans=True, showextrema=True, showmedians=True,
8787
quantiles=[0.05, 0.1, 0.8, 0.9], bw_method=0.5)
8888
axs[1, 4].set_title('Custom violin 11', fontsize=fs)
8989

90-
axs[1, 5].violinplot(data[-1:], pos[-1:], points=200, vert=False, widths=1.1,
91-
showmeans=True, showextrema=True, showmedians=True,
90+
axs[1, 5].violinplot(data[-1:], pos[-1:], points=200, orientation='horizontal',
91+
widths=1.1, showmeans=True, showextrema=True, showmedians=True,
9292
quantiles=[0.05, 0.1, 0.8, 0.9], bw_method=0.5, side='low')
9393

94-
axs[1, 5].violinplot(data[-1:], pos[-1:], points=200, vert=False, widths=1.1,
95-
showmeans=True, showextrema=True, showmedians=True,
94+
axs[1, 5].violinplot(data[-1:], pos[-1:], points=200, orientation='horizontal',
95+
widths=1.1, showmeans=True, showextrema=True, showmedians=True,
9696
quantiles=[0.05, 0.1, 0.8, 0.9], bw_method=0.5, side='high')
9797
axs[1, 5].set_title('Custom violin 12', fontsize=fs)
9898

lib/matplotlib/axes/_axes.py

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8298,9 +8298,10 @@ def matshow(self, Z, **kwargs):
82988298
return im
82998299

83008300
@_preprocess_data(replace_names=["dataset"])
8301-
def violinplot(self, dataset, positions=None, vert=True, widths=0.5,
8301+
def violinplot(self, dataset, positions=None, vert=None, widths=0.5,
83028302
showmeans=False, showextrema=True, showmedians=False,
8303-
quantiles=None, points=100, bw_method=None, side='both'):
8303+
quantiles=None, points=100, bw_method=None, side='both',
8304+
orientation=None):
83048305
"""
83058306
Make a violin plot.
83068307
@@ -8319,8 +8320,14 @@ def violinplot(self, dataset, positions=None, vert=True, widths=0.5,
83198320
vertical violins (or y-axis for horizontal violins).
83208321
83218322
vert : bool, default: True.
8322-
If true, creates a vertical violin plot.
8323-
Otherwise, creates a horizontal violin plot.
8323+
.. deprecated:: 3.10
8324+
Use *orientation* instead.
8325+
8326+
If this is given during the deprecation period, it overrides
8327+
the *orientation* parameter.
8328+
8329+
If True, plots the violins vertically.
8330+
If False, plots the violins horizontally.
83248331
83258332
widths : float or array-like, default: 0.5
83268333
The maximum width of each violin in units of the *positions* axis.
@@ -8355,6 +8362,12 @@ def violinplot(self, dataset, positions=None, vert=True, widths=0.5,
83558362
'both' plots standard violins. 'low'/'high' only
83568363
plots the side below/above the positions value.
83578364
8365+
orientation : {'vertical', 'horizontal'}, default: 'vertical'
8366+
If 'horizontal', plots the violins horizontally.
8367+
Otherwise, plots the violins vertically.
8368+
8369+
.. versionadded:: 3.10
8370+
83588371
data : indexable object, optional
83598372
DATA_PARAMETER_PLACEHOLDER
83608373
@@ -8405,11 +8418,13 @@ def _kde_method(X, coords):
84058418
vpstats = cbook.violin_stats(dataset, _kde_method, points=points,
84068419
quantiles=quantiles)
84078420
return self.violin(vpstats, positions=positions, vert=vert,
8408-
widths=widths, showmeans=showmeans,
8409-
showextrema=showextrema, showmedians=showmedians, side=side)
8421+
orientation=orientation, widths=widths,
8422+
showmeans=showmeans, showextrema=showextrema,
8423+
showmedians=showmedians, side=side)
84108424

8411-
def violin(self, vpstats, positions=None, vert=True, widths=0.5,
8412-
showmeans=False, showextrema=True, showmedians=False, side='both'):
8425+
def violin(self, vpstats, positions=None, vert=None, widths=0.5,
8426+
showmeans=False, showextrema=True, showmedians=False, side='both',
8427+
orientation=None):
84138428
"""
84148429
Draw a violin plot from pre-computed statistics.
84158430
@@ -8448,8 +8463,14 @@ def violin(self, vpstats, positions=None, vert=True, widths=0.5,
84488463
vertical violins (or y-axis for horizontal violins).
84498464
84508465
vert : bool, default: True.
8451-
If true, plots the violins vertically.
8452-
Otherwise, plots the violins horizontally.
8466+
.. deprecated:: 3.10
8467+
Use *orientation* instead.
8468+
8469+
If this is given during the deprecation period, it overrides
8470+
the *orientation* parameter.
8471+
8472+
If True, plots the violins vertically.
8473+
If False, plots the violins horizontally.
84538474
84548475
widths : float or array-like, default: 0.5
84558476
The maximum width of each violin in units of the *positions* axis.
@@ -8469,6 +8490,12 @@ def violin(self, vpstats, positions=None, vert=True, widths=0.5,
84698490
'both' plots standard violins. 'low'/'high' only
84708491
plots the side below/above the positions value.
84718492
8493+
orientation : {'vertical', 'horizontal'}, default: 'vertical'
8494+
If 'horizontal', plots the violins horizontally.
8495+
Otherwise, plots the violins vertically.
8496+
8497+
.. versionadded:: 3.10
8498+
84728499
Returns
84738500
-------
84748501
dict
@@ -8519,6 +8546,24 @@ def violin(self, vpstats, positions=None, vert=True, widths=0.5,
85198546
datashape_message = ("List of violinplot statistics and `{0}` "
85208547
"values must have the same length")
85218548

8549+
if vert is not None:
8550+
_api.warn_deprecated(
8551+
"3.10",
8552+
name="vert: bool",
8553+
alternative="orientation: {'vertical', 'horizontal'}"
8554+
)
8555+
8556+
# vert and orientation parameters are linked until vert's
8557+
# deprecation period expires. If both are selected,
8558+
# vert takes precedence.
8559+
if vert or vert is None and orientation is None:
8560+
orientation = 'vertical'
8561+
elif vert is False:
8562+
orientation = 'horizontal'
8563+
8564+
if orientation is not None:
8565+
_api.check_in_list(['horizontal', 'vertical'], orientation=orientation)
8566+
85228567
# Validate positions
85238568
if positions is None:
85248569
positions = range(1, N + 1)
@@ -8547,7 +8592,7 @@ def violin(self, vpstats, positions=None, vert=True, widths=0.5,
85478592
fillcolor = linecolor = self._get_lines.get_next_color()
85488593

85498594
# Check whether we are rendering vertically or horizontally
8550-
if vert:
8595+
if orientation == 'vertical':
85518596
fill = self.fill_betweenx
85528597
if side in ['low', 'high']:
85538598
perp_lines = functools.partial(self.hlines, colors=linecolor,

lib/matplotlib/axes/_axes.pyi

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,7 @@ class Axes(_AxesBase):
736736
self,
737737
dataset: ArrayLike | Sequence[ArrayLike],
738738
positions: ArrayLike | None = ...,
739-
vert: bool = ...,
739+
vert: bool | None = ...,
740740
widths: float | ArrayLike = ...,
741741
showmeans: bool = ...,
742742
showextrema: bool = ...,
@@ -748,19 +748,21 @@ class Axes(_AxesBase):
748748
| Callable[[GaussianKDE], float]
749749
| None = ...,
750750
side: Literal["both", "low", "high"] = ...,
751+
orientation: Literal["vertical", "horizontal"] | None = ...,
751752
*,
752753
data=...,
753754
) -> dict[str, Collection]: ...
754755
def violin(
755756
self,
756757
vpstats: Sequence[dict[str, Any]],
757758
positions: ArrayLike | None = ...,
758-
vert: bool = ...,
759+
vert: bool | None = ...,
759760
widths: float | ArrayLike = ...,
760761
showmeans: bool = ...,
761762
showextrema: bool = ...,
762763
showmedians: bool = ...,
763764
side: Literal["both", "low", "high"] = ...,
765+
orientation: Literal["vertical", "horizontal"] | None = ...,
764766
) -> dict[str, Collection]: ...
765767

766768
table = mtable.table

lib/matplotlib/pyplot.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4142,7 +4142,7 @@ def triplot(*args, **kwargs):
41424142
def violinplot(
41434143
dataset: ArrayLike | Sequence[ArrayLike],
41444144
positions: ArrayLike | None = None,
4145-
vert: bool = True,
4145+
vert: bool | None = None,
41464146
widths: float | ArrayLike = 0.5,
41474147
showmeans: bool = False,
41484148
showextrema: bool = True,
@@ -4154,6 +4154,7 @@ def violinplot(
41544154
| Callable[[GaussianKDE], float]
41554155
| None = None,
41564156
side: Literal["both", "low", "high"] = "both",
4157+
orientation: Literal["vertical", "horizontal"] | None = None,
41574158
*,
41584159
data=None,
41594160
) -> dict[str, Collection]:
@@ -4169,6 +4170,7 @@ def violinplot(
41694170
points=points,
41704171
bw_method=bw_method,
41714172
side=side,
4173+
orientation=orientation,
41724174
**({"data": data} if data is not None else {}),
41734175
)
41744176

lib/matplotlib/tests/test_axes.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8998,3 +8998,40 @@ def test_boxplot_tick_labels():
89988998
# Test the new tick_labels parameter
89998999
axs[1].boxplot(data, tick_labels=['A', 'B', 'C'])
90009000
assert [l.get_text() for l in axs[1].get_xticklabels()] == ['A', 'B', 'C']
9001+
9002+
9003+
@check_figures_equal(extensions=['png'])
9004+
def test_violinplot_orientation(fig_test, fig_ref):
9005+
# Test the `orientation : {'vertical', 'horizontal'}`
9006+
# parameter and deprecation of `vert: bool`.
9007+
fig, axs = plt.subplots(nrows=1, ncols=3)
9008+
np.random.seed(19680801)
9009+
all_data = [np.random.normal(0, std, 100) for std in range(6, 10)]
9010+
9011+
axs[0].violinplot(all_data) # Default vertical plot.
9012+
# xticks and yticks should be at their default position.
9013+
assert all(axs[0].get_xticks() == np.array(
9014+
[0.5, 1., 1.5, 2., 2.5, 3., 3.5, 4., 4.5]))
9015+
assert all(axs[0].get_yticks() == np.array(
9016+
[-30., -20., -10., 0., 10., 20., 30.]))
9017+
9018+
# Horizontal plot using new `orientation` keyword.
9019+
axs[1].violinplot(all_data, orientation='horizontal')
9020+
# xticks and yticks should be swapped.
9021+
assert all(axs[1].get_xticks() == np.array(
9022+
[-30., -20., -10., 0., 10., 20., 30.]))
9023+
assert all(axs[1].get_yticks() == np.array(
9024+
[0.5, 1., 1.5, 2., 2.5, 3., 3.5, 4., 4.5]))
9025+
9026+
plt.close()
9027+
9028+
# Deprecation of `vert: bool` keyword
9029+
with pytest.warns(mpl.MatplotlibDeprecationWarning,
9030+
match='vert: bool was deprecated in Matplotlib 3.10'):
9031+
# Compare images between a figure that
9032+
# uses vert and one that uses orientation.
9033+
ax_ref = fig_ref.subplots()
9034+
ax_ref.violinplot(all_data, vert=False)
9035+
9036+
ax_test = fig_test.subplots()
9037+
ax_test.violinplot(all_data, orientation='horizontal')

0 commit comments

Comments
 (0)