2017-05-29 4 views
1

私は以下のパンダのデータフレームを持っています。基本的に、7つの異なるアクションカテゴリ、5つの異なるターゲット、各カテゴリには1つ以上の固有のエンドポイントがあり、各エンドポイントはそれぞれのターゲットで一定のスコアを得ています。 合計250のエンドポイントがあります。Python seabornヒートマップグリッド - 予定されているカラムを取らない

action,target,endpoint,score 
Category1,target1,endpoint1,813.0 
Category1,target2,endpoint1,757.0 
Category1,target3,endpoint1,155.0 
Category1,target4,endpoint1,126.0 
Category1,target5,endpoint1,75.5 
Category2,target1,endpoint2,106.0 
Category2,target1,endpoint3,101.0 
Category2,target1,endpoint4,499.0 
Category2,target1,endpoint5,207.0 
Category2,target2,endpoint2,316.0 
Category2,target2,endpoint3,208.0 
Category2,target2,endpoint4,161.0 
Category2,target2,endpoint5,198.0 
<omit> 
Category3,target1,endpoint8,193.0 
Category3,target1,endpoint9,193.0 
Category3,target1,endpoint10,193.0 
Category3,target1,endpoint11,193.0 
Category3,target2,endpoint8,193.0 
Category3,target2,endpoint9,193.0 
<List goes on...> 

ここでは、このデータフレームをカテゴリごとのヒートマップとしてマップしたいと考えました。 したがって、以下のコードでseabronファセットグリッドヒートマップを使用しました。

import pandas as pd 
import matplotlib.pyplot as plt 
import seaborn as sns 

data = pd.read_csv('rawData.csv') 
data = data.drop('Unnamed: 0', 1) 


def facet_heatmap(data, **kwargs): 


    data2 = data.pivot(index="target", columns='endpoint', values='score') 
    ax1 = sns.heatmap(data2, cmap="YlGnBu", linewidths=2) 

    for item in ax1.get_yticklabels(): 
     item.set_rotation(0) 

    for item in ax1.get_xticklabels(): 
     item.set_rotation(70) 


with sns.plotting_context(font_scale=5.5): 

    g = sns.FacetGrid(data, col="action", col_wrap=7, size=5, aspect=0.5) 

cbar_ax = g.fig.add_axes([.92, .3, .02, .4]) 

g = g.map_dataframe(facet_heatmap, cbar=cbar_ax, min=0, vmax=2000) 
# <-- Specify the colorbar axes and limits 

g.set_titles(col_template="{col_name}", fontweight='bold', fontsize=18) 
g.fig.subplots_adjust(right=3) # <-- Add space so the colorbar doesn't overlap the plot 

plt.savefig('seabornPandas.png', dpi=400) 
plt.show() 

実際にヒートマップグリッドを生成します。しかし、問題は、各ヒートマップが何らかの理由で同じ列を使用していることです。下記の添付のスクリーンショットをご覧ください。

As you may notice, it is quite odd. (カラーバーと制限を無視してください。)

これは非常に奇妙です。まず、インデックスは順調ではありません。第2に、各ヒートマップボックスは、最後の3つのエンドポイント(エンドポイント248,249、および250)のみを取ります。これは間違っています。カテゴリ1の場合、エンドポイント1のみを使用する必要があります。私はそこに灰色のボックスを期待していません。

カテゴリ2の場合は、エンドポイント2,3,4,5が必要です。エンドポイントではありません248,249,250

これら2つの問題を解決するにはどうすればよいですか?どんな提案やコメントも大歓迎です。

+0

あなたは 'data2'あなたはそれが見えると思いますlthe道に見えることを確認していましたか? –

+0

x軸の共有をオフにする必要があるように聞こえますが、一般的にxファクタが(交差するのではなく)colファクタ内にネストされていれば、ファセットグリッドでプロットするのはまさに正しい構造ではありません。 – mwaskom

+0

@AndrasDeakはい、データ2が期待どおりに見えることを確認しました。各エンドポイントは、ターゲットごとに表示されます。 –

答えて

0

mwaskomが示唆されているように:あなたの問題を解決するためにsharexパラメータを使用します。

... 

with sns.plotting_context(font_scale=5.5): 

g = sns.FacetGrid(data, col="action", col_wrap=7, size=5, aspect=0.5, 
       sharex=False) 

... 
関連する問題