2017-06-11 18 views
2

私はPython 3とSeabornを使用してカテゴリ・ストリップ・ポットを作成しています(下のコードと画像を参照)。Python matplotlib /ポイント間の接続を伴うSeabornストリップ・スロット

各ストライプポットには2つのデータポイント(各性別ごとに1つ)があります。

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


df = [["city2", "f", 300], 
    ["city2", "m", 39], 
    ["city1", "f", 95], 
    ["city1", "m", 53]] 

df = pd.DataFrame(df, columns = ["city", "gender", "variable"]) 

sns.stripplot(data=df,x='city',hue='gender',y='variable', size=10, linewidth=1) 

私は次の出力enter image description here

を取得しかし、私は男性と女性の点を結ぶ線分を持っていると思います。私はその姿をこのようにしたいと思っています(下の写真を見てください)。しかし、私は手作業でそれらの赤線を描きました。Seabornまたはmatplotlibを使って簡単な方法があるのだろうかと思います。ありがとうございました! enter image description here

+0

あなたは常にあなた自身のラッパーを作ることができます線を引く。 – GWW

答えて

3

あなたはpandas.dataframe.groupbyを使用して、FMのペアのリストを作成し、ペア間のセグメントをプロットすることができます

import numpy as np 
import matplotlib.pyplot as plt 
from matplotlib import collections as mc 
import pandas as pd 
import seaborn as sns 


df = [["city2", "f", 300], 
     ["city2", "m", 39], 
     ["city1", "f", 95], 
     ["city1", "m", 53], 
     ["city4", "f", 200], 
     ["city3", "f", 100], 
     ["city4", "m", 236], 
     ["city3", "m", 20],] 


df = pd.DataFrame(df, columns = ["city", "gender", "variable"]) 


ax = sns.stripplot(data=df,x='city',hue='gender',y='variable', size=10, linewidth=1) 

lines = ([[x, n] for n in group] for x, (_, group) in enumerate(df.groupby(['city'], sort = False)['variable'])) 
lc = mc.LineCollection(lines, colors='red', linewidths=2)  
ax.add_collection(lc) 

sns.plt.show() 

出力:

enter image description here

関連する問題