2016-06-30 4 views
3

マイデータフレーム(パンダの構造)今、私は別のキャンバス上の各機能の箱ひげ図を作りたいenter image description hereパンダボックスプロット

上記のように見えます。分離条件は最初の列です。私はヒストグラム(下のコード)のための同様のプロットがありますが、私はboxplotの作業バージョンを作ることができません。コード上

hist_params = {'normed': True, 'bins': 60, 'alpha': 0.4} 
# create the figure 
fig = plt.figure(figsize=(16, 25)) 
for n, feature in enumerate(features): 
    # add sub plot on our figure 
    ax = fig.add_subplot(features.shape[1] // 5 + 1, 6, n + 1) 
    # define range for histograms by cutting 1% of data from both ends 
    min_value, max_value = numpy.percentile(data[feature], [1, 99]) 
    ax.hist(data.ix[data.is_true_seed.values == 0, feature].values, range=(min_value, max_value), 
      label='ghost', **hist_params) 
    ax.hist(data.ix[data.is_true_seed.values == 1, feature].values, range=(min_value, max_value), 
      label='true', **hist_params) 
    ax.legend(loc='best') 

    ax.set_title(feature) 

(その一部のみを結合している)のような出力を生成する: enter image description here

答えて

5

DataFrame.boxplot()はかなりよく、これを自動化:

import numpy as np 
import pandas as pd 
import matplotlib.pyplot as plt 

df = pd.DataFrame({'is_true_seed': np.random.choice([True, False], 10), 
        'col1': np.random.normal(size=10), 
        'col2': np.random.normal(size=10), 
        'col3': np.random.normal(size=10)}) 

fig, ax = plt.subplots(figsize=(10, 10)) 
df.boxplot(['col1', 'col2', 'col3'], 'is_true_seed', ax) 

最初の引数は、列がプロットするパンダを伝え、 2番目の列はグループ化する(分離条件と呼ぶ)、3番目の列は描画する軸です。

グループ化したい列以外の列をすべて一覧表示するのは面倒ですが、その最初の引数を省略することで回避できます。他の2つの名前を明示的に指定する必要があります。

df.boxplot(by='is_true_seed', ax=ax)