<p>由于<code>tsplot</code>函数不提供直接设置错误条值或更改用于计算错误条值的方法的方法,因此我找到的唯一解决方案是对<code>timeseries</code>模块进行猴子修补:</p>
<pre><code>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
</code></pre>
<p>然后,用标准偏差误差条绘制</p>
<pre><code>ax = sns.tsplot(data, err_style="std_bars", n_boot=0)
</code></pre>
<p>或者</p>
<pre><code>ax = sns.tsplot(data, err_style="std_band", n_boot=0)
</code></pre>
<p>用标准偏差带绘图。</p>
<p>编辑:受<a href="https://stackoverflow.com/a/24890566/2997179">this answer</a>的启发,另一种(可能更明智的)方法是使用以下方法而不是<code>tsplot</code>:</p>
<pre><code>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="")
</code></pre>
<p>Edit2:因为您询问<code>tsplot</code>如何计算其置信区间:它在每个时间点使用<a href="https://en.wikipedia.org/wiki/Bootstrapping_(statistics)#Estimating_the_distribution_of_sample_mean" rel="nofollow noreferrer">bootstrapping to estimate the distribution of the mean value</a>,然后从这些分布中找到低百分位和高百分位值(对应于所使用的置信区间)。默认置信区间为68%——假设正态分布,相当于平均值的±1个标准差。分别为16%和84%。可以通过<code>ci</code>关键字参数更改置信区间。</p>