Replacement for obsolete tsplot

I have a time series with homogeneous samples stored in a numpy array, and I would like to plot their average value using a boot confidence interval. As a rule, I used tsplotfrom Seaborn for this. However, now it is out of date . What should I use for replacement?

The following is a usage example 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)

Note: this is similar to the questions “ Strong tsplot error ” and “ line chart with marine tsplot ”. However, in my case, I really need the Seaborn confidence interval functionality and therefore cannot just use Matplotlib without any inconvenient coding.

+4
source share
1 answer

An example tsplotfrom a question can be easily replicated using matplotlib.

Using standard deviation as an error estimate

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()

enter image description here

Using bootstrapping to evaluate errors

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()

enter image description here


I think the reason this is deprecated is that the use of this function is quite limited, and in most cases you better just build the data that you want to build directly.

+7
source

Source: https://habr.com/ru/post/1690088/


All Articles