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

Skip to content

Commit e0a39ce

Browse files
committed
add violin orientation param
1 parent 9955a7c commit e0a39ce

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
@@ -8350,9 +8350,10 @@ def matshow(self, Z, **kwargs):
83508350

83518351
@_api.make_keyword_only("3.9", "vert")
83528352
@_preprocess_data(replace_names=["dataset"])
8353-
def violinplot(self, dataset, positions=None, vert=True, widths=0.5,
8353+
def violinplot(self, dataset, positions=None, vert=None, widths=0.5,
83548354
showmeans=False, showextrema=True, showmedians=False,
8355-
quantiles=None, points=100, bw_method=None, side='both'):
8355+
quantiles=None, points=100, bw_method=None, side='both',
8356+
orientation=None):
83568357
"""
83578358
Make a violin plot.
83588359
@@ -8371,8 +8372,14 @@ def violinplot(self, dataset, positions=None, vert=True, widths=0.5,
83718372
vertical violins (or y-axis for horizontal violins).
83728373
83738374
vert : bool, default: True.
8374-
If true, creates a vertical violin plot.
8375-
Otherwise, creates a horizontal violin plot.
8375+
.. deprecated:: 3.10
8376+
Use *orientation* instead.
8377+
8378+
If this is given during the deprecation period, it overrides
8379+
the *orientation* parameter.
8380+
8381+
If True, plots the violins vertically.
8382+
If False, plots the violins horizontally.
83768383
83778384
widths : float or array-like, default: 0.5
83788385
The maximum width of each violin in units of the *positions* axis.
@@ -8407,6 +8414,12 @@ def violinplot(self, dataset, positions=None, vert=True, widths=0.5,
84078414
'both' plots standard violins. 'low'/'high' only
84088415
plots the side below/above the positions value.
84098416
8417+
orientation : {'vertical', 'horizontal'}, default: 'vertical'
8418+
If 'horizontal', plots the violins horizontally.
8419+
Otherwise, plots the violins vertically.
8420+
8421+
.. versionadded:: 3.10
8422+
84108423
data : indexable object, optional
84118424
DATA_PARAMETER_PLACEHOLDER
84128425
@@ -8457,12 +8470,14 @@ def _kde_method(X, coords):
84578470
vpstats = cbook.violin_stats(dataset, _kde_method, points=points,
84588471
quantiles=quantiles)
84598472
return self.violin(vpstats, positions=positions, vert=vert,
8460-
widths=widths, showmeans=showmeans,
8461-
showextrema=showextrema, showmedians=showmedians, side=side)
8473+
orientation=orientation, widths=widths,
8474+
showmeans=showmeans, showextrema=showextrema,
8475+
showmedians=showmedians, side=side)
84628476

84638477
@_api.make_keyword_only("3.9", "vert")
8464-
def violin(self, vpstats, positions=None, vert=True, widths=0.5,
8465-
showmeans=False, showextrema=True, showmedians=False, side='both'):
8478+
def violin(self, vpstats, positions=None, vert=None, widths=0.5,
8479+
showmeans=False, showextrema=True, showmedians=False, side='both',
8480+
orientation=None):
84668481
"""
84678482
Draw a violin plot from pre-computed statistics.
84688483
@@ -8501,8 +8516,14 @@ def violin(self, vpstats, positions=None, vert=True, widths=0.5,
85018516
vertical violins (or y-axis for horizontal violins).
85028517
85038518
vert : bool, default: True.
8504-
If true, plots the violins vertically.
8505-
Otherwise, plots the violins horizontally.
8519+
.. deprecated:: 3.10
8520+
Use *orientation* instead.
8521+
8522+
If this is given during the deprecation period, it overrides
8523+
the *orientation* parameter.
8524+
8525+
If True, plots the violins vertically.
8526+
If False, plots the violins horizontally.
85068527
85078528
widths : float or array-like, default: 0.5
85088529
The maximum width of each violin in units of the *positions* axis.
@@ -8522,6 +8543,12 @@ def violin(self, vpstats, positions=None, vert=True, widths=0.5,
85228543
'both' plots standard violins. 'low'/'high' only
85238544
plots the side below/above the positions value.
85248545
8546+
orientation : {'vertical', 'horizontal'}, default: 'vertical'
8547+
If 'horizontal', plots the violins horizontally.
8548+
Otherwise, plots the violins vertically.
8549+
8550+
.. versionadded:: 3.10
8551+
85258552
Returns
85268553
-------
85278554
dict
@@ -8572,6 +8599,24 @@ def violin(self, vpstats, positions=None, vert=True, widths=0.5,
85728599
datashape_message = ("List of violinplot statistics and `{0}` "
85738600
"values must have the same length")
85748601

8602+
if vert is not None:
8603+
_api.warn_deprecated(
8604+
"3.10",
8605+
name="vert: bool",
8606+
alternative="orientation: {'vertical', 'horizontal'}"
8607+
)
8608+
8609+
# vert and orientation parameters are linked until vert's
8610+
# deprecation period expires. If both are selected,
8611+
# vert takes precedence.
8612+
if vert or vert is None and orientation is None:
8613+
orientation = 'vertical'
8614+
elif vert is False:
8615+
orientation = 'horizontal'
8616+
8617+
if orientation is not None:
8618+
_api.check_in_list(['horizontal', 'vertical'], orientation=orientation)
8619+
85758620
# Validate positions
85768621
if positions is None:
85778622
positions = range(1, N + 1)
@@ -8600,7 +8645,7 @@ def violin(self, vpstats, positions=None, vert=True, widths=0.5,
86008645
fillcolor = linecolor = self._get_lines.get_next_color()
86018646

86028647
# Check whether we are rendering vertically or horizontally
8603-
if vert:
8648+
if orientation == 'vertical':
86048649
fill = self.fill_betweenx
86058650
if side in ['low', 'high']:
86068651
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
@@ -739,7 +739,7 @@ class Axes(_AxesBase):
739739
dataset: ArrayLike | Sequence[ArrayLike],
740740
positions: ArrayLike | None = ...,
741741
*,
742-
vert: bool = ...,
742+
vert: bool | None = ...,
743743
widths: float | ArrayLike = ...,
744744
showmeans: bool = ...,
745745
showextrema: bool = ...,
@@ -751,19 +751,21 @@ class Axes(_AxesBase):
751751
| Callable[[GaussianKDE], float]
752752
| None = ...,
753753
side: Literal["both", "low", "high"] = ...,
754+
orientation: Literal["vertical", "horizontal"] | None = ...,
754755
data=...,
755756
) -> dict[str, Collection]: ...
756757
def violin(
757758
self,
758759
vpstats: Sequence[dict[str, Any]],
759760
positions: ArrayLike | None = ...,
760761
*,
761-
vert: bool = ...,
762+
vert: bool | None = ...,
762763
widths: float | ArrayLike = ...,
763764
showmeans: bool = ...,
764765
showextrema: bool = ...,
765766
showmedians: bool = ...,
766767
side: Literal["both", "low", "high"] = ...,
768+
orientation: Literal["vertical", "horizontal"] | None = ...,
767769
) -> dict[str, Collection]: ...
768770

769771
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
@@ -9034,3 +9034,40 @@ def test_latex_pie_percent(fig_test, fig_ref):
90349034

90359035
ax1 = fig_ref.subplots()
90369036
ax1.pie(data, autopct=r"%1.0f\%%", textprops={'usetex': True})
9037+
9038+
9039+
@check_figures_equal(extensions=['png'])
9040+
def test_violinplot_orientation(fig_test, fig_ref):
9041+
# Test the `orientation : {'vertical', 'horizontal'}`
9042+
# parameter and deprecation of `vert: bool`.
9043+
fig, axs = plt.subplots(nrows=1, ncols=3)
9044+
np.random.seed(19680801)
9045+
all_data = [np.random.normal(0, std, 100) for std in range(6, 10)]
9046+
9047+
axs[0].violinplot(all_data) # Default vertical plot.
9048+
# xticks and yticks should be at their default position.
9049+
assert all(axs[0].get_xticks() == np.array(
9050+
[0.5, 1., 1.5, 2., 2.5, 3., 3.5, 4., 4.5]))
9051+
assert all(axs[0].get_yticks() == np.array(
9052+
[-30., -20., -10., 0., 10., 20., 30.]))
9053+
9054+
# Horizontal plot using new `orientation` keyword.
9055+
axs[1].violinplot(all_data, orientation='horizontal')
9056+
# xticks and yticks should be swapped.
9057+
assert all(axs[1].get_xticks() == np.array(
9058+
[-30., -20., -10., 0., 10., 20., 30.]))
9059+
assert all(axs[1].get_yticks() == np.array(
9060+
[0.5, 1., 1.5, 2., 2.5, 3., 3.5, 4., 4.5]))
9061+
9062+
plt.close()
9063+
9064+
# Deprecation of `vert: bool` keyword
9065+
with pytest.warns(mpl.MatplotlibDeprecationWarning,
9066+
match='vert: bool was deprecated in Matplotlib 3.10'):
9067+
# Compare images between a figure that
9068+
# uses vert and one that uses orientation.
9069+
ax_ref = fig_ref.subplots()
9070+
ax_ref.violinplot(all_data, vert=False)
9071+
9072+
ax_test = fig_test.subplots()
9073+
ax_test.violinplot(all_data, orientation='horizontal')

0 commit comments

Comments
 (0)