鉴于下面的数据框,我想创建一个包含两个子图的图形,每个半球一个。 Plts应该显示针对差异的均值。
df = pd.DataFrame({'id':[1,1,1,1,2,2,2,2],
'eye':['l','r','l','r','l','r','l','r'],
'trial':[1,1,2,2,1,1,2,2],
'S':[2,2,3,3,5,5,7,7],
'I':[2,2,1,1,4,4,3,3]})
df = df.melt(id_vars=['id','eye','trial'],
value_vars=['S','I'],
var_name='Hemisphere',
value_name='Thickness')
df = df.pivot_table(index=['id','eye','Hemisphere'],
columns='trial',
values='Thickness')
df['diffs'] = df[1] - df[2]
df['means'] = np.mean([df[1], df[2]], axis=0)
df = df.unstack(level=2)
df.plot('means','diffs',subplots=True,kind='scatter')
答案 0 :(得分:2)
groupby
与axis=1
axes = df[['diffs', 'means']].groupby(axis=1, level=1).plot.scatter('means', 'diffs')
groupby
更好地控制
colors = iter('gr')
fig, axes = plt.subplots(2, 1, sharex=True, figsize=(6, 8))
for i, (k, d) in enumerate(df.groupby(axis=1, level=1)):
d.xs(k, axis=1, level=1).plot.scatter(
'means', 'diffs', title=k, ax=axes[i], c=next(colors))
fig.tight_layout()