Timeseries Always Need A Distribution


In [1]:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import NullFormatter
%matplotlib inline
plt.rcParams['figure.figsize'] = (12, 6)

In [2]:
def ts_hist(x, y, title=None, xlabel=None, ylabel=None, bins=30, alpha=0.6, color='b'):
    split = 0.1
    plt.figure(1)
    axScatter = plt.axes([split + 0.01, 0, 1-split, 1])
    if title is not None:
        plt.title(title)
    axHisty = plt.axes([0, 0, split, 1])
    if xlabel is not None:
        axScatter.set_xlabel(xlabel)
    if ylabel is not None:
        axHisty.set_ylabel(ylabel)
    axScatter.yaxis.set_major_formatter(NullFormatter())
    axHisty.xaxis.set_major_formatter(NullFormatter())
    axScatter.plot(x, y, alpha=alpha, c=color)
    axScatter.grid()
    axScatter.set_xlim( (np.min(x), np.max(x)) )
    axScatter.set_ylim( (np.min(y), np.max(y)) )
    axHisty.hist(y, bins=bins, alpha=alpha, orientation='horizontal', color=color)
    lim = axHisty.get_xlim()
    axHisty.set_xlim((lim[1], lim[0]))
    axHisty.grid()
    axHisty.set_ylim( axScatter.get_ylim() )

In [3]:
x = np.linspace(0,3,1000)
y = np.cos(8 * np.pi * x) * np.exp(-x) + 0.1 * np.random.randn(len(x))

kw = {
    'title': 'Damped Oscillations', 
    'xlabel': 'time (sec)', 
    'ylabel': 'Displacement (m)',
    'color': 'g',
}
ts_hist(x, y, **kw)