Skip to content

Instantly share code, notes, and snippets.

@mpharrigan
Created July 28, 2017 20:47
Show Gist options
  • Save mpharrigan/a4375afa71d560dbdf3a0d96552a1cd5 to your computer and use it in GitHub Desktop.
Save mpharrigan/a4375afa71d560dbdf3a0d96552a1cd5 to your computer and use it in GitHub Desktop.
Violin plot with inner median
"""Violin plot with just the median drawn
By default, when plotting a split violin plot, the inner
box plot is for both splits. You can change this with `inner='quartile'`
to plot the quartiles for each split, but by default it uses a dashed
line (small dashes) for the 25 and 75-%ile and another dashed line
(big dashes) for the median, which is very ugly and cluttered.
This patching the `draw_quartiles` function to just draw the median
as a solid line.
This only works with seaborn 0.8. It does not work with seaborn 0.7
(init args have changed in `_ViolinPlotter`, as is their wont)
"""
from seaborn.categorical import _ViolinPlotter
import numpy as np
from matplotlib import pyplot as plt
class MyVPlot(_ViolinPlotter):
def draw_quartiles(self, ax, data, support, density, center, split=False):
"""Draw the quartiles as lines at width of density."""
q25, q50, q75 = np.percentile(data, [25, 50, 75])
self.draw_to_density(ax, center, q50, support, density, split,
linewidth=self.linewidth, )
def my_violinplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None,
bw="scott", cut=2, scale="area", scale_hue=True, gridsize=100,
width=.8, inner="box", split=False, dodge=True, orient=None,
linewidth=None, color=None, palette=None, saturation=.75,
ax=None, **kwargs):
plotter = MyVPlot(x, y, hue, data, order, hue_order,
bw, cut, scale, scale_hue, gridsize,
width, inner, split, dodge, orient, linewidth,
color, palette, saturation)
if ax is None:
ax = plt.gca()
plotter.plot(ax)
return ax
@valosekj
Copy link

Excellent! Thank you 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment