2017-08-07 13 views
1

Iは、以下パンダのデータフレームdf有する:私のデータセットの複数行プロットを作成するには?

df = pd.DataFrame(columns=["Event1", "Event2", "Event3"], 
           data=[[15,1,22], 
             [16,1.26,80], 
             [27,0,15]]) 

df = df.set_index([["Series1", "Series2", "Series3"]]) 

をIは、Y軸は対応する数値であるべきであるが、Event1Event2Event3を含むX軸と複数行のプロットを作成します。 3つのシリーズ:Series1,Series2,Series3があります。

どのように私はsns.pointplot(x=???, y=???, hue=???,data=df)xyhueを定義することができますか?

plt.figure(figsize=(12,8)) 
ax = sns.pointplot(x=???, y=???, hue=???,data=df) 
ax.grid(b=True, which='major', color='#d3d3d3', linewidth=1.0) 
ax.grid(b=True, which='minor', color='#d3d3d3', linewidth=0.5) 
plt.show() 
+0

'sns.pointplot'を使用するか、希望のプロットを得るための最も簡単な方法を取得する目的ですか? – ImportanceOfBeingErnest

+0

@ImportanceOfBeingErnest:理想的には、シーボーンのポイントプロットを使用したいと考えています。しかし私は純粋な 'matplotlib'も受け入れます。 – Dinosaurius

答えて

1

再編成DF:

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

df = pd.DataFrame(columns=["Event1", "Event2", "Event3"], 
           data=[[15,1,22], 
             [16,1.26,80], 
             [27,0,15]]) 
df = df.set_index([["Series1", "Series2", "Series3"]]) 
print(df) 

# reorganize df to classic table 
df2=df.stack().reset_index() 
df2.columns = ['Series','Event','Values'] 
print(df2) 

plt.figure(figsize=(12,8)) 
ax = sns.pointplot(x='Event', y='Values', hue='Series',data=df2) 
ax.grid(b=True, which='major', color='#d3d3d3', linewidth=1.0) 
ax.grid(b=True, which='minor', color='#d3d3d3', linewidth=0.5) 
plt.show() 

enter image description here

がDF2:

Series Event Values 
0 Series1 Event1 15.00 
1 Series1 Event2 1.00 
2 Series1 Event3 22.00 
3 Series2 Event1 16.00 
4 Series2 Event2 1.26 
5 Series2 Event3 80.00 
6 Series3 Event1 27.00 
7 Series3 Event2 0.00 
8 Series3 Event3 15.00 
1

私はseabornでそれを行う方法がわからないですけどmatplolibと、それはこのようなものです:この場合

for i in df.index.values: 
    plt.plot(list(df.loc[i])) 
plt.show() 

X斧はYながら値0、1、2を取りますaxはあなたの行の値をとります。最後にplt.show()を実行して、すべての図を1つの図にまとめるだけです。海軍では、同じようにかなりうまくいくはずです。あなたが好きな古典的なデータフレームとプロットにピボットテーブルで

1

希望のプロットを得るための最も簡単な方法がありますdf.T.plot()

import pandas as pd 

df = pd.DataFrame(columns=["Event1", "Event2", "Event3"], 
           data=[[15,1,22], 
             [16,1.26,80], 
             [27,0,15]]) 

df = df.set_index([["Series1", "Series2", "Series3"]]) 

df.T.plot() 

enter image description here

関連する問題