Python中Seaborn tsplot函数中的标准偏差和误差线 [英] standard deviation and errors bars in seaborn tsplot function in Python
问题描述
Seaborn 如何计算其误差线?例子:
How does Seaborn compute its error bars? example:
import numpy as np; np.random.seed(22)
import seaborn as sns; sns.set(color_codes=True)
x = np.linspace(0, 15, 31)
data = np.sin(x) + np.random.rand(10, 31) + np.random.randn(10, 1)
ax = sns.tsplot(data=data, err_style="ci_bars")
plt.show()
ci_bars(或ci_bands)是如何计算的?
how are the ci_bars (or ci_bands) calculated?
还可以用ci_bars样式制作 tsplot
图,其中误差线或条带对应于每个时间点的值的标准偏差吗?(而不是均值或引导程序的标准误差)
also, is it possible to make tsplot
plot in ci_bars style where the error bars or bands correspond to the standard deviation of the values at each time point? (and not standard error of mean, or bootstraps)
推荐答案
在 Seaborn v0.8.0(2017年7月)中添加了使用误差线显示标准偏差而不是自举置信区间的功能在大多数统计函数中,通过放置 ci="sd".所以这现在有效
In Seaborn v0.8.0 (July 2017) was added the ability to use error bars to show standard deviations rather than bootstrap confidence intervals in most statistical functions by putting ci="sd". So this now works
sns.tsplot(data=data, ci="sd")
对于以前的 Seaborn 版本,绘制标准偏差的解决方法可能是在 seaborn tsplot 之上使用 matplotlib errorbar:
For previous Seaborn versions a workaround for plotting standard deviation could be to use matplotlib errorbar on top of seaborn tsplot:
import numpy as np;
import seaborn as sns;
import pandas as pd
import matplotlib.pyplot as plt
# create a group of time series
num_samples = 90
group_size = 10
x = np.linspace(0, 10, num_samples)
group = np.sin(x) + np.linspace(0, 2, num_samples) + np.random.rand(group_size, num_samples) + np.random.randn(group_size, 1)
df = pd.DataFrame(group.T, index=range(0,num_samples))
# plot time series with seaborn
ax = sns.tsplot(data=df.T.values) #, err_style="unit_traces")
# Add std deviation bars to the previous plot
mean = df.mean(axis=1)
std = df.std(axis=1)
ax.errorbar(df.index, mean, yerr=std, fmt='-o') #fmt=None to plot bars only
plt.show()
这篇关于Python中Seaborn tsplot函数中的标准偏差和误差线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!