使用 matplotlib 和 seaborn 在多元时间序列图中突出显示时间间隔

Highlight time interval in multivariate time-series plot using matplotlib and seaborn

我想用时间间隔注释多元时间序列图(每种类型的注释用颜色显示)。

数据概览

示例数据集如下所示:

            metrik_0  metrik_1  metrik_2  geospatial_id  topology_id  \
2020-01-01 -0.848009  1.305906  0.924208             12            4   
2020-01-01 -0.516120  0.617011  0.623065              8            3   
2020-01-01  0.762399 -0.359898 -0.905238             19            3   
2020-01-01  0.708512 -1.502019 -2.677056              8            4   
2020-01-01  0.249475  0.590983 -0.677694             11            3   

            cohort_id  device_id  
2020-01-01          1          1  
2020-01-01          1          9  
2020-01-01          2         13  
2020-01-01          2          8  
2020-01-01          1         12  

标签看起来像这样:

cohort_id marker_type               start                 end
0          1           a 2020-01-02 00:00:00                 NaT
1          1           b 2020-01-04 05:00:00 2020-01-05 16:00:00
2          1           a 2020-01-06 00:00:00                 NaT

想要的结果

我考虑过使用 seaborn/matplotlib 来完成这项任务。

到目前为止我已经转过来了:

%pylab inline
import seaborn as sns; sns.set()
import matplotlib.dates as mdates

aut_locator = mdates.AutoDateLocator(minticks=3, maxticks=7)
aut_formatter = mdates.ConciseDateFormatter(aut_locator)

g = df[df['cohort_id'] == 1].plot(figsize=(8,8))
g.xaxis.set_major_locator(aut_locator)
g.xaxis.set_major_formatter(aut_formatter)
plt.show()

这是相当混乱的。 我担心,将指标(多变量数据)拟合到单个图中是不可能的。 它应该由每一列分面。 然而,这又需要重塑 seaborn FacetGrid 的数据框才能工作,这也感觉不太对——尤其是当 cohort_id 中的元素(时间序列)数量变大时。 如果 FacetGrid 是正确的方法,那么第一部分将是:https://seaborn.pydata.org/examples/timeseries_facets.html,但标签仍然会丢失。

如何添加标签? 第一部分应该怎么完成?

期望结果的示例: https://imgur.com/9J1EcmI,即其中之一

每个指标值

示例数据的代码

数据集是从下面的代码片段生成的:

import pandas as pd
import numpy as np

import random
random_seed = 47
np.random.seed(random_seed)
random.seed(random_seed)
def generate_df_for_device(n_observations, n_metrics, device_id, geo_id, topology_id, cohort_id):
        df = pd.DataFrame(np.random.randn(n_observations,n_metrics), index=pd.date_range('2020', freq='H', periods=n_observations))
        df.columns = [f'metrik_{c}' for c in df.columns]
        df['geospatial_id'] = geo_id
        df['topology_id'] = topology_id
        df['cohort_id'] = cohort_id
        df['device_id'] = device_id
        return df
    
def generate_multi_device(n_observations, n_metrics, n_devices, cohort_levels, topo_levels):
    results = []
    for i in range(1, n_devices +1):
        #print(i)
        r = random.randrange(1, n_devices)
        cohort = random.randrange(1, cohort_levels)
        topo = random.randrange(1, topo_levels)
        df_single_dvice = generate_df_for_device(n_observations, n_metrics, i, r, topo, cohort)
        results.append(df_single_dvice)
        #print(r)
    return pd.concat(results)

# hourly data, 1 week of data
n_observations = 7 * 24
n_metrics = 3
n_devices = 20
cohort_levels = 3
topo_levels = 5

df = generate_multi_device(n_observations, n_metrics, n_devices, cohort_levels, topo_levels)
df = df.sort_index()
df.head()

marker_labels = pd.DataFrame({'cohort_id':[1,1, 1], 'marker_type':['a', 'b', 'a'], 'start':['2020-01-2', '2020-01-04 05', '2020-01-06'], 'end':[np.nan, '2020-01-05 16', np.nan]})
marker_labels['start'] = pd.to_datetime(marker_labels['start'])
marker_labels['end'] = pd.to_datetime(marker_labels['end'])

一般来说,您可以对水平条带使用 plt.fill_between,对垂直条带使用 plt.fill_betweenx。对于“bands-within-bands”,您只需调用该方法两次即可。

使用您的数据的基本示例如下所示。我使用固定值作为波段的位置,但您可以将它们放在主数据框上并在循环内动态引用它们。

import matplotlib.pyplot as plt

fig, ax = plt.subplots(3 ,figsize=(20, 9), sharex=True)
plt.subplots_adjust(hspace=0.2)

metriks = ["metrik_0", "metrik_1", "metrik_2"]
colors = ['#66c2a5', '#fc8d62', '#8da0cb'] #Set2 palette hexes

for i, metric in enumerate(metriks):
    
    df[[metric]].plot(ax=ax[i], color=colors[i], legend=None)
    ax[i].set_ylabel(metric)

    ax[i].fill_betweenx(y=[-3, 3], x1="2020-01-04 05:00:00",
                        x2="2020-01-05 16:00:00", color='gray', alpha=0.2)
    ax[i].fill_betweenx(y=[-3, 3], x1="2020-01-04 15:00:00",
                        x2="2020-01-05 00:00:00", color='gray', alpha=0.4)