Matplotlib > Violin plots

Name
Code
Output
gaussian = numpy.random.normal(loc=1.0, scale=0.1,size = 1000)
matplotlib.pyplot.violinplot(gaussian)
matplotlib.pyplot.show()

       
           Violin plot (basic)
       
    
# Data
gauss1 = numpy.random.normal(loc=1.0, scale=10.0,size = 1000)
gauss2 = numpy.random.normal(loc=5.0, scale=7.0,size = 1000)
gauss3 = numpy.random.normal(loc=10.0, scale=5.0,size = 1000)
# New Figure
fig = matplotlib.pyplot.figure(figsize=(7,4),facecolor='silver')
# New Axes
ax = fig.add_subplot(111,facecolor='azure')
# Add grid
ax.grid(color='b', linestyle='-', linewidth=0.5)
# Plot the data
bw_plot = ax.violinplot([gauss1,gauss2,gauss3],positions=[0,2,4],
                        vert=True,showmeans=True,showextrema=True,showmedians=True)
for item in bw_plot['bodies']:
    item.set_facecolor('red')
    item.set_edgecolor('grey')
    item.set_alpha(0.8)
ax.set(title="Plot title", xlabel="x axis", ylabel="y axis") 
labels = ['A','B','C','D','E']
ax.set_xticks(numpy.arange(0, len(labels)))
ax.set_xticklabels(labels)
# Fit the plot layout
matplotlib.pyplot.tight_layout()
# Show the plot
matplotlib.pyplot.show()

       
           Violin plot (customized)