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

Skip to content

Commit 78ac503

Browse files
committed
add violin orientation param
1 parent 428f569 commit 78ac503

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
@@ -8302,9 +8302,10 @@ def matshow(self, Z, **kwargs):
83028302
return im
83038303

83048304
@_preprocess_data(replace_names=["dataset"])
8305-
def violinplot(self, dataset, positions=None, vert=True, widths=0.5,
8305+
def violinplot(self, dataset, positions=None, vert=None, widths=0.5,
83068306
showmeans=False, showextrema=True, showmedians=False,
8307-
quantiles=None, points=100, bw_method=None, side='both'):
8307+
quantiles=None, points=100, bw_method=None, side='both',
8308+
orientation=None):
83088309
"""
83098310
Make a violin plot.
83108311
@@ -8323,8 +8324,14 @@ def violinplot(self, dataset, positions=None, vert=True, widths=0.5,
83238324
vertical violins (or y-axis for horizontal violins).
83248325
83258326
vert : bool, default: True.
8326-
If true, creates a vertical violin plot.
8327-
Otherwise, creates a horizontal violin plot.
8327+
.. deprecated:: 3.10
8328+
Use *orientation* instead.
8329+
8330+
If this is given during the deprecation period, it overrides
8331+
the *orientation* parameter.
8332+
8333+
If True, plots the violins vertically.
8334+
If False, plots the violins horizontally.
83288335
83298336
widths : float or array-like, default: 0.5
83308337
The maximum width of each violin in units of the *positions* axis.
@@ -8359,6 +8366,12 @@ def violinplot(self, dataset, positions=None, vert=True, widths=0.5,
83598366
'both' plots standard violins. 'low'/'high' only
83608367
plots the side below/above the positions value.
83618368
8369+
orientation : {'vertical', 'horizontal'}, default: 'vertical'
8370+
If 'horizontal', plots the violins horizontally.
8371+
Otherwise, plots the violins vertically.
8372+
8373+
.. versionadded:: 3.10
8374+
83628375
data : indexable object, optional
83638376
DATA_PARAMETER_PLACEHOLDER
83648377
@@ -8409,11 +8422,13 @@ def _kde_method(X, coords):
84098422
vpstats = cbook.violin_stats(dataset, _kde_method, points=points,
84108423
quantiles=quantiles)
84118424
return self.violin(vpstats, positions=positions, vert=vert,
8412-
widths=widths, showmeans=showmeans,
8413-
showextrema=showextrema, showmedians=showmedians, side=side)
8425+
orientation=orientation, widths=widths,
8426+
showmeans=showmeans, showextrema=showextrema,
8427+
showmedians=showmedians, side=side)
84148428

8415-
def violin(self, vpstats, positions=None, vert=True, widths=0.5,
8416-
showmeans=False, showextrema=True, showmedians=False, side='both'):
8429+
def violin(self, vpstats, positions=None, vert=None, widths=0.5,
8430+
showmeans=False, showextrema=True, showmedians=False, side='both',
8431+
orientation=None):
84178432
"""
84188433
Draw a violin plot from pre-computed statistics.
84198434
@@ -8452,8 +8467,14 @@ def violin(self, vpstats, positions=None, vert=True, widths=0.5,
84528467
vertical violins (or y-axis for horizontal violins).
84538468
84548469
vert : bool, default: True.
8455-
If true, plots the violins vertically.
8456-
Otherwise, plots the violins horizontally.
8470+
.. deprecated:: 3.10
8471+
Use *orientation* instead.
8472+
8473+
If this is given during the deprecation period, it overrides
8474+
the *orientation* parameter.
8475+
8476+
If True, plots the violins vertically.
8477+
If False, plots the violins horizontally.
84578478
84588479
widths : float or array-like, default: 0.5
84598480
The maximum width of each violin in units of the *positions* axis.
@@ -8473,6 +8494,12 @@ def violin(self, vpstats, positions=None, vert=True, widths=0.5,
84738494
'both' plots standard violins. 'low'/'high' only
84748495
plots the side below/above the positions value.
84758496
8497+
orientation : {'vertical', 'horizontal'}, default: 'vertical'
8498+
If 'horizontal', plots the violins horizontally.
8499+
Otherwise, plots the violins vertically.
8500+
8501+
.. versionadded:: 3.10
8502+
84768503
Returns
84778504
-------
84788505
dict
@@ -8523,6 +8550,24 @@ def violin(self, vpstats, positions=None, vert=True, widths=0.5,
85238550
datashape_message = ("List of violinplot statistics and `{0}` "
85248551
"values must have the same length")
85258552

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

85538598
# Check whether we are rendering vertically or horizontally
8554-
if vert:
8599+
if orientation == 'vertical':
85558600
fill = self.fill_betweenx
85568601
if side in ['low', 'high']:
85578602
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
@@ -9022,3 +9022,40 @@ def test_latex_pie_percent(fig_test, fig_ref):
90229022

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

0 commit comments

Comments
 (0)