Created
December 4, 2020 04:26
-
-
Save kwcooper/913482a058813f8a52323f57f2cf9000 to your computer and use it in GitHub Desktop.
create a violin plot with matplotlib
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import matplotlib.pyplot as plt | |
# kwc 201208 | |
# Create synthetic data for each plot | |
np.random.seed(10) | |
synData_1 = np.random.normal(100, 10, 200) | |
synData_2 = np.random.normal(80, 30, 200) | |
synData_3 = np.random.normal(90, 20, 200) | |
synData_4 = np.random.normal(70, 25, 200) | |
all_data = [synData_1, synData_2, synData_3, synData_4] | |
# Plot it | |
fig = plt.figure(figsize=(10,8)) | |
vp = plt.violinplot(all_data,showmeans=True, | |
showmedians=False, showextrema=True) | |
# if using ax methods | |
#ax = fig.add_axes([0,0,1,1]) | |
# vp = ax.violinplot(all_data) | |
# to edit the color | |
for pc in vp['bodies']: | |
pc.set_facecolor('red') | |
pc.set_edgecolor('black') | |
pc.set_linewidth(1) | |
pc.set_alpha(0.2) | |
# color the violin statistics marks: | |
for partname in ('cbars','cmins','cmaxes','cmeans'): # if medians,'cmedians' | |
vp_ = vp[partname] | |
vp_.set_edgecolor('k') | |
vp_.set_linewidth(1) | |
plt.title('Violin Plot example', size=20) | |
plt.xlabel("y label", size=14) | |
plt.ylabel("x label", size=14) | |
plt.xticks(size=14) | |
plt.yticks(size=14) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment