<jupyter_start><jupyter_text>Visualising Patterns in Time-Series Data: 2D Overlay PlotsThis is the example notebook for the medium article found here:  https://towardsdatascience.com/visualising-patterns-in-time-series-data-2d-overlay-plots-fb8920139044  An Initial Look at the Data<jupyter_code># load the data
df = pd.read_csv('./data/Ti_data.csv', index_col=0, parse_dates=True)

# plot the data
fig, ax = plt.subplots(1, 1, figsize=(10, 6))

df.plot(ax=ax, color='C0', legend=None)

ax.set_ylim(17.75, 22)
ax.set_ylabel('$[˚C]$')
ax.set_title('Internal Temperature $(T_i)$');<jupyter_output><empty_output><jupyter_text>Visualising Diurnal Patterns<jupyter_code># load the data
df = pd.read_csv('./data/Ti_data.csv', index_col=0, parse_dates=True)

# data pre-processing: pivot
df['date'] = df.index.normalize()
df['time'] = df.index.time.astype(str)
df_pivot = df.pivot(index='date', columns='time', values='Ti')

# plot the data
fig, ax = plt.subplots(1, 1, figsize=(10, 6))

# NOTE: we are plotting the transposed DataFrame
df_pivot.T.plot(ax=ax, color='C0', legend=False)

ax.set_xlim([0, 47])
ax.set_ylim(17.75, 22)
ax.set_ylabel('$[˚C]$')
ax.set_title('Internal Temperature $(T_i)$');<jupyter_output><empty_output><jupyter_text>Getting the Alpha Right<jupyter_code># load the data
df = pd.read_csv('./data/Ti_data.csv', index_col=0, parse_dates=True)

# data pre-processing: pivot
df['date'] = df.index.normalize()
df['time'] = df.index.time.astype(str)
df_pivot = df.pivot(index='date', columns='time', values='Ti')

# plot the data
fig, ax = plt.subplots(1, 1, figsize=(10, 6))

# NOTE: we are plotting the transposed DataFrame
df_pivot.T.plot(ax=ax, color='C0', alpha=0.1, legend=False)

ax.set_xlim([0, 47])
ax.set_ylim(17.75, 22)
ax.set_ylabel('$[˚C]$')
ax.set_title('Internal Temperature $(T_i)$');<jupyter_output><empty_output><jupyter_text>Incorporating Domain Knowledge<jupyter_code># load the data
df = pd.read_csv('./data/Ti_data.csv', index_col=0, parse_dates=True)

# data pre-processing: pivot
df['date'] = df.index.normalize()
df['time'] = df.index.time.astype(str)
df_pivot = df.pivot(index='date', columns='time', values='Ti')

# roll the DataFrame back by 36 half-hours
# NOTE: the dateindex refers to the heating day
# as seen by the heating system
df_roll = pd.DataFrame(index=df_pivot.index, 
                       columns=np.roll(df_pivot.columns, -36), 
                       data=np.roll(df_pivot.values, -36))

# plot the data
fig, ax = plt.subplots(1, 1, figsize=(10, 6))

# NOTE: we are plotting the transposed DataFrame
df_roll.T.plot(ax=ax, color='C0', alpha=0.1, legend=False)

ax.set_xlim([0, 47])
ax.set_ylim(17.75, 22)
ax.set_ylabel('$[˚C]$')
ax.set_title('Internal Temperature $(T_i)$');<jupyter_output><empty_output><jupyter_text>Visualising Diurnal and Weekly Patterns<jupyter_code>day_colors = { 'Monday': 'C0', 
               'Tuesday': 'C1',
               'Wednesday': 'C1',
               'Thursday': 'C1',
               'Friday': 'C1',
               'Saturday': 'C2',
               'Sunday': 'C3'}

# load the data
df = pd.read_csv('./data/Ti_data.csv', index_col=0, parse_dates=True)

# data pre-processing: pivot
df['date'] = df.index.normalize()
df['time'] = df.index.time.astype(str)
df_pivot = df.pivot(index='date', columns='time', values='Ti')

# roll the DataFrame back by 36 half-hours
# NOTE: the dateindex refers to the heating day
# as seen by the heating system
df_roll = pd.DataFrame(index=np.roll(df_pivot.index, 0), 
                       columns=np.roll(df_pivot.columns, -36), 
                       data=np.roll(df_pivot.values, -36))

# plot the data
fig, ax = plt.subplots(1, 1, figsize=(10, 6))

for day_name, color in day_colors.items():
    # NOTE: we are plotting the transposed DataFrame
    df_roll[df_roll.index.day_name() == day_name].T.plot(
        ax=ax, color=color, alpha=0.1, legend=None)

ax.set_xlim([0, 47])
ax.set_ylim(17.75, 22)

# make the heating schedule clear
ax.axvline(30, alpha=0.25, color='black', linestyle='--')
ax.axvline(40, alpha=0.25, color='black', linestyle='--')
ax.axvline(42, alpha=0.25, color='black', linestyle='--')

ax.set_ylabel('$[˚C]$')
ax.set_title('Internal Temperature $(T_i)$');

# make the legend pretty
from matplotlib.lines import Line2D
custom_lines = [Line2D([0], [0], color='C0', lw=2),
                Line2D([0], [0], color='C1', lw=2),
                Line2D([0], [0], color='C2', lw=2),
                Line2D([0], [0], color='C3', lw=2)]

ax.legend(custom_lines, ['Mon', 'Tue-Fri', 'Sat', 'Sun']);<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17582658200131376, 'fraction_numerical': 0.032844317932997594, 'mean_word_length': 4.431629013079667, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '643457', 'license': 'MIT', 'n_tokens_mistral': 1862, 'n_tokens_neox': 1736, 'n_words': 367, 'path': '2d_overlay_plots/2d_overlay_plots.ipynb', 'repo_name': 'czagoni/medium-visualising-patterns', 'size': '4567'}