Movatterモバイル変換


[0]ホーム

URL:


  1. matplotlib-gallery
  2. ipynb
Notebook

Sebastian Raschka

back to thematplotlib-gallery athttps://github.com/rasbt/matplotlib-gallery

In [1]:
%load_ext watermark
In [2]:
%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

In [3]:
%matplotlib inline


Boxplots in matplotlib

Sections







Simple Boxplot

[back to top]

In [7]:
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()


Black and white Boxplot

[back to top]

In [8]:
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()


Horizontal Boxplot

[back to top]

In [13]:
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()


Filled and cylindrical boxplots

[back to top]

In [10]:
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()


Boxplots with custom fill colors

[back to top]

In [31]:
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

[back to top]

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.

In [9]:
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()
In [ ]:

[8]ページ先頭

©2009-2025 Movatter.jp