-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a new orientation
parameter to Violinplot and deprecate vert
#27998
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
``violinplot`` and ``violin`` *vert* parameter | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
The parameter *vert: bool* has been deprecated on `~.Axes.violinplot` and | ||
`~.Axes.violin`. | ||
It will be replaced by *orientation: {"vertical", "horizontal"}* for API | ||
consistency. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
``violinplot`` and ``violin`` orientation parameter | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
Violinplots have a new parameter *orientation: {"vertical", "horizontal"}* | ||
to change the orientation of the plot. This will replace the deprecated | ||
*vert: bool* parameter. | ||
|
||
|
||
.. plot:: | ||
:include-source: true | ||
:alt: Example of creating 4 horizontal violinplots. | ||
|
||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
|
||
fig, ax = plt.subplots() | ||
np.random.seed(19680801) | ||
all_data = [np.random.normal(0, std, 100) for std in range(6, 10)] | ||
|
||
ax.violinplot(all_data, orientation='horizontal') | ||
plt.show() |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8350,9 +8350,10 @@ def matshow(self, Z, **kwargs): | |
|
||
@_api.make_keyword_only("3.9", "vert") | ||
@_preprocess_data(replace_names=["dataset"]) | ||
def violinplot(self, dataset, positions=None, vert=True, widths=0.5, | ||
def violinplot(self, dataset, positions=None, vert=None, widths=0.5, | ||
showmeans=False, showextrema=True, showmedians=False, | ||
quantiles=None, points=100, bw_method=None, side='both'): | ||
quantiles=None, points=100, bw_method=None, side='both', | ||
orientation=None): | ||
""" | ||
Make a violin plot. | ||
|
||
|
@@ -8371,8 +8372,14 @@ def violinplot(self, dataset, positions=None, vert=True, widths=0.5, | |
vertical violins (or y-axis for horizontal violins). | ||
|
||
vert : bool, default: True. | ||
If true, creates a vertical violin plot. | ||
Otherwise, creates a horizontal violin plot. | ||
.. deprecated:: 3.10 | ||
Use *orientation* instead. | ||
|
||
If this is given during the deprecation period, it overrides | ||
the *orientation* parameter. | ||
|
||
If True, plots the violins vertically. | ||
If False, plots the violins horizontally. | ||
|
||
widths : float or array-like, default: 0.5 | ||
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, | |
'both' plots standard violins. 'low'/'high' only | ||
plots the side below/above the positions value. | ||
|
||
orientation : {'vertical', 'horizontal'}, default: 'vertical' | ||
If 'horizontal', plots the violins horizontally. | ||
Otherwise, plots the violins vertically. | ||
|
||
.. versionadded:: 3.10 | ||
|
||
data : indexable object, optional | ||
DATA_PARAMETER_PLACEHOLDER | ||
|
||
|
@@ -8457,12 +8470,14 @@ def _kde_method(X, coords): | |
vpstats = cbook.violin_stats(dataset, _kde_method, points=points, | ||
quantiles=quantiles) | ||
return self.violin(vpstats, positions=positions, vert=vert, | ||
widths=widths, showmeans=showmeans, | ||
showextrema=showextrema, showmedians=showmedians, side=side) | ||
orientation=orientation, widths=widths, | ||
showmeans=showmeans, showextrema=showextrema, | ||
showmedians=showmedians, side=side) | ||
|
||
@_api.make_keyword_only("3.9", "vert") | ||
def violin(self, vpstats, positions=None, vert=True, widths=0.5, | ||
showmeans=False, showextrema=True, showmedians=False, side='both'): | ||
def violin(self, vpstats, positions=None, vert=None, widths=0.5, | ||
showmeans=False, showextrema=True, showmedians=False, side='both', | ||
orientation=None): | ||
""" | ||
Draw a violin plot from pre-computed statistics. | ||
|
||
|
@@ -8501,8 +8516,14 @@ def violin(self, vpstats, positions=None, vert=True, widths=0.5, | |
vertical violins (or y-axis for horizontal violins). | ||
|
||
vert : bool, default: True. | ||
If true, plots the violins vertically. | ||
Otherwise, plots the violins horizontally. | ||
.. deprecated:: 3.10 | ||
Use *orientation* instead. | ||
|
||
If this is given during the deprecation period, it overrides | ||
the *orientation* parameter. | ||
|
||
If True, plots the violins vertically. | ||
If False, plots the violins horizontally. | ||
|
||
widths : float or array-like, default: 0.5 | ||
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, | |
'both' plots standard violins. 'low'/'high' only | ||
plots the side below/above the positions value. | ||
|
||
orientation : {'vertical', 'horizontal'}, default: 'vertical' | ||
If 'horizontal', plots the violins horizontally. | ||
Otherwise, plots the violins vertically. | ||
|
||
.. versionadded:: 3.10 | ||
|
||
Returns | ||
------- | ||
dict | ||
|
@@ -8572,6 +8599,24 @@ def violin(self, vpstats, positions=None, vert=True, widths=0.5, | |
datashape_message = ("List of violinplot statistics and `{0}` " | ||
"values must have the same length") | ||
|
||
if vert is not None: | ||
_api.warn_deprecated( | ||
"3.10", | ||
name="vert: bool", | ||
alternative="orientation: {'vertical', 'horizontal'}" | ||
) | ||
|
||
# vert and orientation parameters are linked until vert's | ||
# deprecation period expires. If both are selected, | ||
# vert takes precedence. | ||
if vert or vert is None and orientation is None: | ||
orientation = 'vertical' | ||
elif vert is False: | ||
orientation = 'horizontal' | ||
|
||
if orientation is not None: | ||
_api.check_in_list(['horizontal', 'vertical'], orientation=orientation) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not following the logic here completely. After whatever mangling vert and orientation, shouldn't we have a normalized
Can't we simply
and default There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes it should default to vertical instead of None (it even says so in the docstring) so there was no reason for it other than making it more convoluted. That code change is great, I'll add it to the next commit |
||
|
||
# Validate positions | ||
if positions is None: | ||
positions = range(1, N + 1) | ||
|
@@ -8600,7 +8645,7 @@ def violin(self, vpstats, positions=None, vert=True, widths=0.5, | |
fillcolor = linecolor = self._get_lines.get_next_color() | ||
|
||
# Check whether we are rendering vertically or horizontally | ||
if vert: | ||
if orientation == 'vertical': | ||
saranti marked this conversation as resolved.
Show resolved
Hide resolved
|
||
fill = self.fill_betweenx | ||
if side in ['low', 'high']: | ||
perp_lines = functools.partial(self.hlines, colors=linecolor, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(
optional
is synonymous withdefault: None
)