Skip to content
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 inner="median" option to violin plot #3733

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions seaborn/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,7 @@ def plot_violins(
iter_vars = [self.orient, "hue"]
value_var = {"x": "y", "y": "x"}[self.orient]

inner_options = ["box", "quart", "stick", "point", None]
inner_options = ["box", "quart", "median", "stick", "point", None]
_check_argument("inner", inner_options, inner, prefix=True)
_check_argument("density_norm", ["area", "count", "width"], density_norm)

Expand Down Expand Up @@ -1093,16 +1093,22 @@ def vars_to_key(sub_vars):
lines = mpl.collections.LineCollection(segments, **kws)
ax.add_collection(lines, autolim=False)

elif inner.startswith("quart"):
stats = np.percentile(obs, [25, 50, 75])
elif inner.startswith("quart") or inner.startswith("median"):
median_dashes = (2.5, 1)
if inner.startswith("median"):
q = [50]
dashes = [median_dashes]
else:
q = [25, 50, 75]
dashes = [(1.25, .75), median_dashes, (1.25, .75)]
stats = np.percentile(obs, q)
pos0 = np.interp(stats, data[value_var], data[self.orient] - offsets[0])
pos1 = np.interp(stats, data[value_var], data[self.orient] + offsets[1])
pos_pts = np.stack([inv_pos(pos0), inv_pos(pos1)])
val_pts = np.stack([inv_val(stats), inv_val(stats)])
segments = np.stack([pos_pts, val_pts]).transpose(2, 0, 1)
if self.orient == "y":
segments = segments[:, ::-1, :]
dashes = [(1.25, .75), (2.5, 1), (1.25, .75)]
for i, segment in enumerate(segments):
kws = {
"color": linecolor,
Expand Down Expand Up @@ -1810,11 +1816,12 @@ def violinplot(
{palette}
{saturation}
{fill}
inner : {{"box", "quart", "point", "stick", None}}
inner : {{"box", "quart", "median" "point", "stick", None}}
Representation of the data in the violin interior. One of the following:

- `"box"`: draw a miniature box-and-whisker plot
- `"quart"`: show the quartiles of the data
- `"median"`: show the median of the data
- `"point"` or `"stick"`: show each observation
split : bool
Show an un-mirrored distribution, alternating sides when using `hue`.
Expand Down