替换过时的tsplot [英] Replacement for deprecated tsplot

查看:774
本文介绍了替换过时的tsplot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个将均匀样本保存到numpy数组中的时间序列,我想用自举的置信区间绘制它们的平均值.通常,我使用Seaborn的tsplot来完成此任务.但是,现在已弃用.我应该用什么替代品?

I have a time-series with uniform samples save to a numpy array and I'd like to plot their mean value with a bootstrapped confidence interval. Typically, I've used tsplot from Seaborn to accomplish this. However, this is now being deprecated. What am I supposed to use a replacement?

以下是根据Seaborn文档改编而成的示例用法:

Here is an example usage below adapted from the Seaborn documentation:

x = np.linspace(0, 15, 31)
data = np.sin(x) + np.random.rand(10, 31) + np.random.randn(10, 1)
sns.tsplot(data)

注意:这类似于问题"季节性tsplot错误"和"带有seaborn tsplot的多线图".但是,就我而言,我实际上需要Seaborn的置信区间功能,因此,如果没有一些笨拙的编码,就不能简单地使用Matplotlib.

Note: this is similar to questions "Seaborn tsplot error" and "Multi-line chart with seaborn tsplot". However, in my case, I actually need the confidence interval functionality of Seaborn and thus cannot simply use Matplotlib without some awkward coding.

推荐答案

使用matplotlib可以轻松地复制问题中的示例tsplot.

The example tsplot from the question can easily be replicated using matplotlib.

import numpy as np; np.random.seed(1)
import matplotlib.pyplot as plt
import seaborn as sns

x = np.linspace(0, 15, 31)
data = np.sin(x) + np.random.rand(10, 31) + np.random.randn(10, 1)


fig, (ax,ax2) = plt.subplots(ncols=2, sharey=True)
ax = sns.tsplot(data=data,ax=ax, ci="sd")

def tsplot(ax, data,**kw):
    x = np.arange(data.shape[1])
    est = np.mean(data, axis=0)
    sd = np.std(data, axis=0)
    cis = (est - sd, est + sd)
    ax.fill_between(x,cis[0],cis[1],alpha=0.2, **kw)
    ax.plot(x,est,**kw)
    ax.margins(x=0)

tsplot(ax2, data)

ax.set_title("sns.tsplot")
ax2.set_title("custom tsplot")

plt.show()

import numpy as np; np.random.seed(1)
from scipy import stats
import matplotlib.pyplot as plt
import seaborn as sns

x = np.linspace(0, 15, 31)
data = np.sin(x) + np.random.rand(10, 31) + np.random.randn(10, 1)


fig, (ax,ax2) = plt.subplots(ncols=2, sharey=True)
ax = sns.tsplot(data=data,ax=ax)

def bootstrap(data, n_boot=10000, ci=68):
    boot_dist = []
    for i in range(int(n_boot)):
        resampler = np.random.randint(0, data.shape[0], data.shape[0])
        sample = data.take(resampler, axis=0)
        boot_dist.append(np.mean(sample, axis=0))
    b = np.array(boot_dist)
    s1 = np.apply_along_axis(stats.scoreatpercentile, 0, b, 50.-ci/2.)
    s2 = np.apply_along_axis(stats.scoreatpercentile, 0, b, 50.+ci/2.)
    return (s1,s2)

def tsplotboot(ax, data,**kw):
    x = np.arange(data.shape[1])
    est = np.mean(data, axis=0)
    cis = bootstrap(data)
    ax.fill_between(x,cis[0],cis[1],alpha=0.2, **kw)
    ax.plot(x,est,**kw)
    ax.margins(x=0)

tsplotboot(ax2, data)

ax.set_title("sns.tsplot")
ax2.set_title("custom tsplot")

plt.show()

我不建议使用此功能的原因恰恰是此功能的使用受到限制,在大多数情况下,最好直接绘制要绘制的数据.

I guess the reason this is deprecated is exactly that the use of this function is rather limited and in most cases you are better off just plotting the data you want to plot directly.

这篇关于替换过时的tsplot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆