Note

Go to the endto download the full example code.

Violin plot customization#

This example demonstrates how to fully customize violin plots. The first plotshows the default style by providing only the data. The second plot firstlimits what Matplotlib draws with additional keyword arguments. Then asimplified representation of a box plot is drawn on top. Lastly, the styles ofthe artists of the violins are modified.

For more information on violin plots, the scikit-learn docs have a greatsection:https://scikit-learn.org/stable/modules/density.html

importmatplotlib.pyplotaspltimportnumpyasnpdefadjacent_values(vals,q1,q3):upper_adjacent_value=q3+(q3-q1)*1.5upper_adjacent_value=np.clip(upper_adjacent_value,q3,vals[-1])lower_adjacent_value=q1-(q3-q1)*1.5lower_adjacent_value=np.clip(lower_adjacent_value,vals[0],q1)returnlower_adjacent_value,upper_adjacent_valuedefset_axis_style(ax,labels):ax.set_xticks(np.arange(1,len(labels)+1),labels=labels)ax.set_xlim(0.25,len(labels)+0.75)ax.set_xlabel('Sample name')# create test datanp.random.seed(19680801)data=[sorted(np.random.normal(0,std,100))forstdinrange(1,5)]fig,(ax1,ax2,ax3)=plt.subplots(nrows=1,ncols=3,figsize=(9,3),sharey=True)ax1.set_title('Default violin plot')ax1.set_ylabel('Observed values')ax1.violinplot(data)ax2.set_title('Set colors of violins')ax2.set_ylabel('Observed values')ax2.violinplot(data,facecolor=[('yellow',0.3),('blue',0.3),('red',0.3),('green',0.3)],linecolor='black',)# Note that when passing a sequence of colors, the method will repeat the sequence if# less colors are provided than data distributions.ax3.set_title('Customized violin plot')parts=ax3.violinplot(data,showmeans=False,showmedians=False,showextrema=False,facecolor='#D43F3A',linecolor='black')forpcinparts['bodies']:pc.set_edgecolor('black')pc.set_linewidth(1)pc.set_alpha(1)quartile1,medians,quartile3=np.percentile(data,[25,50,75],axis=1)whiskers=np.array([adjacent_values(sorted_array,q1,q3)forsorted_array,q1,q3inzip(data,quartile1,quartile3)])whiskers_min,whiskers_max=whiskers[:,0],whiskers[:,1]inds=np.arange(1,len(medians)+1)ax3.scatter(inds,medians,marker='o',color='white',s=30,zorder=3)ax3.vlines(inds,quartile1,quartile3,color='k',linestyle='-',lw=5)ax3.vlines(inds,whiskers_min,whiskers_max,color='k',linestyle='-',lw=1)# set style for the axeslabels=['A','B','C','D']foraxin[ax1,ax2,ax3]:set_axis_style(ax,labels)plt.subplots_adjust(bottom=0.15,wspace=0.05)plt.show()
Default violin plot, Set colors of violins, Customized violin plot

Tags:plot-type: violindomain: statistics

References

The use of the following functions, methods, classes and modules is shownin this example:

Gallery generated by Sphinx-Gallery