back to thematplotlib-gallery
athttps://github.com/rasbt/matplotlib-gallery
%load_ext watermark
%watermark -u -v -d -p matplotlib,numpy
Last updated: 26/08/2014 CPython 3.4.1IPython 2.1.0matplotlib 1.4.0numpy 1.8.2
%matplotlib inline
importmatplotlib.pyplotaspltimportnumpyasnpall_data=[np.random.normal(0,std,100)forstdinrange(1,4)]fig=plt.figure(figsize=(8,6))plt.boxplot(all_data,notch=False,# box instead of notch shapesym='rs',# red squares for outliersvert=True)# vertical box aligmnentplt.xticks([y+1foryinrange(len(all_data))],['x1','x2','x3'])plt.xlabel('measurement x')t=plt.title('Box plot')plt.show()
importmatplotlib.pyplotaspltimportnumpyasnpall_data=[np.random.normal(0,std,100)forstdinrange(1,4)]fig=plt.figure(figsize=(8,6))bplot=plt.boxplot(all_data,notch=False,# box instead of notch shapesym='rs',# red squares for outliersvert=True)# vertical box aligmnentplt.xticks([y+1foryinrange(len(all_data))],['x1','x2','x3'])plt.xlabel('measurement x')forcomponentsinbplot.keys():forlineinbplot[components]:line.set_color('black')# black linest=plt.title('Black and white box plot')plt.show()
importmatplotlib.pyplotaspltimportnumpyasnpall_data=[np.random.normal(0,std,100)forstdinrange(1,4)]fig=plt.figure(figsize=(8,6))plt.boxplot(all_data,notch=False,# box instead of notch shapesym='rs',# red squares for outliersvert=False)# horizontal box aligmnentplt.yticks([y+1foryinrange(len(all_data))],['x1','x2','x3'])plt.ylabel('measurement x')t=plt.title('Horizontal Box plot')plt.show()
importmatplotlib.pyplotaspltimportnumpyasnpall_data=[np.random.normal(0,std,100)forstdinrange(1,4)]fig=plt.figure(figsize=(8,6))plt.boxplot(all_data,notch=True,# notch shapesym='bs',# blue squares for outliersvert=True,# vertical box aligmnentpatch_artist=True)# fill with colorplt.xticks([y+1foryinrange(len(all_data))],['x1','x2','x3'])plt.xlabel('measurement x')t=plt.title('Box plot')plt.show()
importmatplotlib.pyplotaspltimportnumpyasnpall_data=[np.random.normal(0,std,100)forstdinrange(1,4)]fig=plt.figure(figsize=(8,6))bplot=plt.boxplot(all_data,notch=False,# notch shapevert=True,# vertical box aligmnentpatch_artist=True)# fill with colorcolors=['pink','lightblue','lightgreen']forpatch,colorinzip(bplot['boxes'],colors):patch.set_facecolor(color)plt.xticks([y+1foryinrange(len(all_data))],['x1','x2','x3'])plt.xlabel('measurement x')t=plt.title('Box plot')plt.show()
Violin plots are closely related to Tukey's (1977) box plots but add useful information such as the distribution of the sample data (density trace).
Violin plots were added inmatplotlib 1.4.
importmatplotlib.pyplotaspltimportnumpyasnpfig,axes=plt.subplots(nrows=1,ncols=2,figsize=(12,5))all_data=[np.random.normal(0,std,100)forstdinrange(6,10)]#fig = plt.figure(figsize=(8,6))axes[0].violinplot(all_data,showmeans=False,showmedians=True)axes[0].set_title('violin plot')axes[1].boxplot(all_data,)axes[1].set_title('box plot')# adding horizontal grid linesforaxinaxes:ax.yaxis.grid(True)ax.set_xticks([y+1foryinrange(len(all_data))],)ax.set_xlabel('xlabel')ax.set_ylabel('ylabel')plt.setp(axes,xticks=[y+1foryinrange(len(all_data))],xticklabels=['x1','x2','x3','x4'],)plt.show()