gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 21:06:31 25 4
gpt4 key购买 nike

我有一个简单的 pandas 表中的数据,其中包含两列:深度和数据。每个深度有多个样本。使用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 轴的标准差?

最佳答案

我找到了解决方案。我从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的代码并引入无意的错误。

关于python - 切换seaborn x 和y 轴,但计算原始方向的标准偏差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55362200/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com