あ 箱ひげ図 としても知られています ウィスカープロット は、最小値、第 1 四分位値、中央値、第 3 四分位値、最大値などのプロパティを持つデータ値のセットの概要を表示するために作成されます。箱ひげ図では、第 1 四分位から第 3 四分位までボックスが作成され、中央値でボックスを通る垂直線も表示されます。ここで、x 軸はプロットされるデータを示し、y 軸は頻度分布を示します。
箱ひげ図の作成
matplotlib ライブラリの matplotlib.pyplot モジュールは、箱ひげ図を作成できる boxplot() 関数を提供します。
構文:
matplotlib.pyplot.boxplot(data, notch=なし, vert=なし, patch_artist=なし, widths=なし)
パラメーター:
| 属性 | 価値 |
|---|---|
| データ | プロットする配列または配列のシーケンス |
| ノッチ | オプションのパラメータはブール値を受け入れます |
| 緑 | オプションのパラメータは、水平プロットと垂直プロットに対してそれぞれブール値 false と true を受け入れます。 |
| ブートストラップ | オプションのパラメータは int を受け入れ、ノッチ付き箱ひげ図の周囲の間隔を指定します |
| ユーザーメディアン | オプションのパラメータは、データと互換性のある配列または配列次元のシーケンスを受け入れます |
| ポジション | オプションのパラメータは配列を受け取り、ボックスの位置を設定します |
| 幅 | オプションのパラメータは配列を受け取り、ボックスの幅を設定します |
| パッチアーティスト | ブール値を持つオプションのパラメータ |
| ラベル | 文字列のシーケンスは各データセットのラベルを設定します |
| 平均線 | オプションでブール値を指定すると、平均線をボックスの全幅としてレンダリングしようとします |
| 注文 | オプションのパラメータは箱ひげ図の順序を設定します |
ax.boxplot() メソッドに指定されるデータ値は、Numpy 配列、Python リスト、または配列のタプルにすることができます。 numpy.random.normal() を使用してランダム データを作成し、平均、標準偏差、および必要な値の数を引数として受け取る箱ひげ図を作成しましょう。
例:
Python3
Linux コマンド
# Import libraries> import> matplotlib.pyplot as plt> import> numpy as np> # Creating dataset> np.random.seed(>10>)> data>=> np.random.normal(>100>,>20>,>200>)> fig>=> plt.figure(figsize>=>(>10>,>7>))> # Creating plot> plt.boxplot(data)> # show plot> plt.show()> |
>
>
出力:

箱ひげ図のカスタマイズ
matplotlib.pyplot.boxplot() は、箱ひげ図に無限のカスタマイズの可能性を提供します。 notch = True 属性は箱ひげ図にノッチ形式を作成します。patch_artist = True は箱ひげ図を色で塗りつぶします。異なる箱に異なる色を設定できます。vert = 0 属性は水平箱ひげ図を作成します。ラベルは数値データセットと同じ次元を取ります。
例 1:
Python3
# Import libraries> import> matplotlib.pyplot as plt> import> numpy as np> # Creating dataset> np.random.seed(>10>)> data_1>=> np.random.normal(>100>,>10>,>200>)> data_2>=> np.random.normal(>90>,>20>,>200>)> data_3>=> np.random.normal(>80>,>30>,>200>)> data_4>=> np.random.normal(>70>,>40>,>200>)> data>=> [data_1, data_2, data_3, data_4]> fig>=> plt.figure(figsize>=>(>10>,>7>))> # Creating axes instance> ax>=> fig.add_axes([>0>,>0>,>1>,>1>])> # Creating plot> bp>=> ax.boxplot(data)> # show plot> plt.show()> |
SQLサーバーピボット
>
>
出力:

例 2: いくつかのカスタマイズを加えて上記のプロットを変更してみましょう。
カイリー・ジェンナーは何歳ですか
Python3
# Import libraries> import> matplotlib.pyplot as plt> import> numpy as np> # Creating dataset> np.random.seed(>10>)> data_1>=> np.random.normal(>100>,>10>,>200>)> data_2>=> np.random.normal(>90>,>20>,>200>)> data_3>=> np.random.normal(>80>,>30>,>200>)> data_4>=> np.random.normal(>70>,>40>,>200>)> data>=> [data_1, data_2, data_3, data_4]> fig>=> plt.figure(figsize>=>(>10>,>7>))> ax>=> fig.add_subplot(>111>)> # Creating axes instance> bp>=> ax.boxplot(data, patch_artist>=> True>,> >notch>=>'True'>, vert>=> 0>)> colors>=> [>'#0000FF'>,>'#00FF00'>,> >'#FFFF00'>,>'#FF00FF'>]> for> patch, color>in> zip>(bp[>'boxes'>], colors):> >patch.set_facecolor(color)> # changing color and linewidth of> # whiskers> for> whisker>in> bp[>'whiskers'>]:> >whisker.>set>(color>=>'#8B008B'>,> >linewidth>=> 1.5>,> >linestyle>=>':'>)> # changing color and linewidth of> # caps> for> cap>in> bp[>'caps'>]:> >cap.>set>(color>=>'#8B008B'>,> >linewidth>=> 2>)> # changing color and linewidth of> # medians> for> median>in> bp[>'medians'>]:> >median.>set>(color>=>'red'>,> >linewidth>=> 3>)> # changing style of fliers> for> flier>in> bp[>'fliers'>]:> >flier.>set>(marker>=>'D'>,> >color>=>'#e7298a'>,> >alpha>=> 0.5>)> > # x-axis labels> ax.set_yticklabels([>'data_1'>,>'data_2'>,> >'data_3'>,>'data_4'>])> # Adding title> plt.title(>'Customized box plot'>)> # Removing top axes and right axes> # ticks> ax.get_xaxis().tick_bottom()> ax.get_yaxis().tick_left()> > # show plot> plt.show()> |
>
>
出力:
