Python中seaborn tsplot函数的标准偏差和误差条
Seaborn如何计算其误差条?例如:Python中seaborn tsplot函数的标准偏差和误差条,python,matplotlib,seaborn,Python,Matplotlib,Seaborn,Seaborn如何计算其误差条?例如: 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")
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_条(或ci_带)
此外,是否可以使
tsplot
以ci_bars样式绘制,其中误差条或带对应于每个时间点的值的标准偏差?(而不是标准平均误差或引导)由于tsplot
函数不提供直接设置误差条值或更改用于计算误差条值的方法的方法,因此我找到的唯一解决方案是对时间序列
模块进行猴子补丁:
import seaborn.timeseries
def _plot_std_bars(*args, central_data=None, ci=None, data=None, **kwargs):
std = data.std(axis=0)
ci = np.asarray((central_data - std, central_data + std))
kwargs.update({"central_data": central_data, "ci": ci, "data": data})
seaborn.timeseries._plot_ci_bars(*args, **kwargs)
def _plot_std_band(*args, central_data=None, ci=None, data=None, **kwargs):
std = data.std(axis=0)
ci = np.asarray((central_data - std, central_data + std))
kwargs.update({"central_data": central_data, "ci": ci, "data": data})
seaborn.timeseries._plot_ci_band(*args, **kwargs)
seaborn.timeseries._plot_std_bars = _plot_std_bars
seaborn.timeseries._plot_std_band = _plot_std_band
然后,用标准差绘制误差条
ax = sns.tsplot(data, err_style="std_bars", n_boot=0)
或
用标准偏差带绘图
编辑:受此启发,另一种(可能更明智)方法是使用以下方法,而不是tsplot
:
import pandas as pd
import seaborn as sns
df = pd.DataFrame.from_dict({
"mean": data.mean(axis=0),
"std": data.std(axis=0)
}).reset_index()
g = sns.FacetGrid(df, size=6)
ax = g.map(plt.errorbar, "index", "mean", "std")
ax.set(xlabel="", ylabel="")
Edit2:因为您询问了tsplot
如何计算其置信区间:它在每个时间点使用,然后从这些分布中找到低百分位值和高百分位值(对应于使用的置信区间)。默认置信区间为68%–假设正态分布,相当于平均值的±一个标准偏差。相应的低百分位数和高百分位数分别为16%和84%。您可以通过ci
关键字参数来更改置信区间。在Seaborn v0.8.0(2017年7月)中,添加了使用误差条显示标准偏差的功能,而不是通过输入ci=“sd”在大多数统计函数中引导置信区间。所以这现在起作用了
sns.tsplot(data=data, ci="sd")
对于以前的Seaborn版本绘制标准偏差的解决方法可以是在Seaborn tsplot顶部使用matplotlib errorbar:
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()
@mwaskom:我的另一个问题是如何在
tsplot
中绘制反映标准偏差的条带,而不是平均值的标准误差或自举估计值。这可能吗?因为我的数据引导太窄,而stdev更具代表性。谢谢Martin,很好的解决方法。不幸的是,python解释器在“*args”参数之后抱怨语法
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()