Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
iris = sns.load_dataset('iris')
f, ax = plt.subplots(figsize=(10, 8))
species_list = np.unique(iris.species)
color_list = ['r', 'g', 'b']
data = [np.array(iris[iris.species==s].sepal_length) for s in species_list]
v1 = ax.violinplot(data, vert=True, showextrema=False, points=100, bw_method=0.3)
for b,c in zip(v1['bodies'], color_list):
m = np.mean(b.get_paths()[0].vertices[:, 0])
b.get_paths()[0].vertices[:, 0] = np.clip(b.get_paths()[0].vertices[:, 0], m, np.inf)
b.set_color(c)
b.set_edgecolor('k')
b.set_linewidth(2)
b.set_alpha(0.6)
for n in range(len(species_list)):
ax.plot(0.1*np.random.uniform(size=len(data[n]))+n+0.85, data[n],
color_list[n], marker='.', ms=10, linestyle='', alpha=0.5)
for n in range(len(species_list)):
ax.errorbar(n+1.03, np.mean(data[n]), yerr=np.sqrt(np.var(data[n])), color='k')
ax.plot(n+1.03, np.mean(data[n]), 'k.', ms=20)
fontsize = 24
ax.set_ylim([4, 8.5])
ax.set_ylabel('Sepal Length', fontsize=fontsize)
ax.set_yticks(range(4,9))
ax.set_yticklabels(range(4,9), fontsize=fontsize-4)
ax.set_xlim([0.5, 3.5])
ax.set_xlabel('Species', fontsize=fontsize)
ax.set_xticks([1,2,3])
ax.set_xticklabels(species_list, fontsize=fontsize-4)
plt.show()
@spopham
Copy link
Author

spopham commented Feb 21, 2018

half_violin

@navotnaor
Copy link

navotnaor commented Jan 29, 2019

Hey Sara,
This is a really cool script, but i was having some difficulty in changing it to work with only 2 levels for x (instead of 3).
Any chance you could help?
Thanks!

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