half (not split!) violin plots in seaborn
Currently seaborn offers functionality for split violinplots by setting
split=True, according to a
hue variable. I would like to make a 'half' violin plot, i.e. a plot where half of each violin is omitted. Such a plot depicts something similar to a pdf for each continuous variable, plotted on one side of each vertical line of each categorical variable only.
I have managed to trick
seaborn to plot this with an extra data point outside the plotted range of values and an extra dummy hue, but I would like to know if this can be done without actually altering the dataset, e.g. within
For instance, this graph:
Was created by this snippet:
# imports import pandas as pd import seaborn as sns import matplotlib.pyplot as plt # load dataset from seaborn datalist = sns.get_dataset_names() dataset_name = 'iris' if dataset_name in datalist: df = sns.load_dataset(dataset_name) else: print("Dataset with name: " + dataset_name + " was not found in the available datasets online by seaborn.") # prepare data df2 = df.append([-999,-999,-999,-999,'setosa']) df2['huecol'] = 0.0 df2['huecol'].iloc[-1]= -999 # plot fig = plt.figure(figsize=(6,6)) sns.violinplot(x='species',y="sepal_width", split=True, hue ='huecol', inner = 'quartile', palette="pastel", data=df2, legend=False) plt.title('iris') # remove hue legend leg = plt.gca().legend() leg.remove() plt.ylim([1,5.0]) plt.show()
The answer is simply, no, it's not possible with seaborn without tricking it into thinking there is a
This answer shows how to do it in matplotlib and in principle the same can be applied to seaborn violinplots as well, namely to cut out half of the violin path.