1 回答
TA贡献1796条经验 获得超4个赞
由于FacetGrid产生自己的图形,lmplot不能在轴内使用。您将需要根据需要绘制尽可能多的regplots。
from io import StringIO
import pandas as pd
df_string="""time\tsex\tage\tval1\tval2\n1\tM\t18\t0.285837375\t4.402793733\n
2\tM\t18\t0.234239365\t2.987464305\n
3\tM\t18\t0.820418465\t3.23991295\n
4\tM\t18\t0.826027695\t9.707366329\n
5\tM\t18\t0.625449525\t2.971235344\n
6\tM\t18\t0.485980081\t5.517575471\n
7\tM\t18\t0.136163546\t3.620177216\n
8\tM\t18\t0.784944053\t5.116294718\n
9\tM\t18\t0.981526403\t6.348155198\n
10\tM\t18\t0.822237037\t4.682176522\n
1\tF\t22\t0.104339381\t5.434133736\n
2\tF\t22\t0.788797127\t0.843869877\n
3\tF\t22\t0.997986894\t8.765048753\n
4\tF\t22\t0.51167857\t2.054679646\n
5\tF\t22\t0.328416139\t6.581617426\n
6\tF\t22\t0.317804112\t1.584234393\n
7\tF\t22\t0.489944956\t8.564257177\n
8\tF\t22\t0.207348127\t1.346020575\n
9\tF\t22\t0.727347344\t7.487993859\n
10\tF\t22\t0.252917798\t8.822904862\n
11\tF\t22\t0.690106636\t6.728470474\n
12\tF\t22\t0.508078197\t2.489437246\n"""
df = pd.read_csv(StringIO(df_string), sep='\t')
import seaborn as sns
import numpy as np
import matplotlib.pylab as plt
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
fig, ax = plt.subplots()
for (n, grp) in df.groupby("sex"):
sns.regplot(x='time',y='val1', x_estimator=np.mean,
data=grp, logx= True, truncate=True)
ax.xaxis.set_label_text('t [sec]')
ax.set(yscale="log")
axins = inset_axes(ax, "30%", "40%" ,loc="lower right", borderpad=3)
for (n, grp) in df.groupby("sex"):
sns.regplot(x='time',y='val1', x_estimator=np.mean,
data=grp, truncate=True, ax=axins)
plt.show()
添加回答
举报