切换原始x和y轴,但计算原始方向的标准偏差

时间:2019-03-26 16:40:42

标签: python seaborn

我在一个简单的熊猫表中有两列数据:深度和基准。每个深度有多个样本。使用seaborn的relplot,我可以使用以下方法生成一个漂亮的数据图:

import seaborn as sns
sns.relplot(x='depth', y='datum', ci='sd', kind='line', data=myData)

这按预期工作。但是,深度在y轴上更有意义,因为它更忠实地代表了地球。如果我告诉seaborn这样交换轴:

sns.relplot(y='depth', x='datum', ci='sd', kind='line', data=myData)

当然不起作用,因为标准偏差是相对于x轴计算的。有没有办法交换轴,但相对于现在的y轴计算和绘制标准偏差?

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。我从seaborn的来源复制了相关的片段,并对其进行了更改。

grouped = myData['myColumn'].groupby(myData['depth'])
est = grouped.agg("mean")
sd = grouped.std()
cis = pd.DataFrame(np.c_[est - sd, est + sd], index=est.index, columns=["low", "high"]).stack()
if cis.notnull().any():
    cis = cis.unstack().reindex(est.index)
else:
    cis = None
x = est.index
y = est
y_ci = cis
x, y = np.asarray(x), np.asarray(y)
low, high = np.asarray(y_ci["low"]), np.asarray(y_ci["high"])
fig = plt.figure(figsize=(8, 8))
ax = fig.gca()
plt.plot(y, x)
plt.fill_betweenx(x, low, high, alpha=0.2)
plt.gca().invert_yaxis()
plt.xlabel('My Column [units]')
plt.ylabel('Depth [m]')

某些变量有些多余,但这是为了避免更改seaborn的代码并引入无意的错误。