Download our e-book of Introduction To Python
Sahil Parrvez
3 years ago
import numpy as np
import matplotlib.pyplot as plt
# generate some random data
sample1= np.random.normal(5, 90, 1000)
sample2= np.random.normal(5, 95, 1000)
sample3= np.random.normal(5, 100, 1000)
sample4= np.random.normal(5, 95, 1000)
sample= list([sample1, sample2, sample3, sample4])
fig, ax = plt.subplots()
# build a violin plot
ax.violinplot(sample, showmeans=False, showmedians=True)
# add title and axis labels
ax.set_title('violin plot')
ax.set_xlabel('x-axis')
ax.set_ylabel('y-axis')
# add x-tick labels
xticklabels = ['sample 1', 'sample 2', 'sample 3', 'sample 4']
ax.set_xticks([1,2,3,4])
ax.set_xticklabels(xticklabels)
# add horizontal grid lines
ax.yaxis.grid(True)
# show the plot
plt.show()