【发布时间】:2021-01-29 01:01:49
【问题描述】:
我想用时间间隔注释多变量时间序列图(每种类型的注释都有颜色)。
数据概览
示例数据集如下所示:
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
想要的结果
- cohort_id 的所有时间序列的多变量图
- 标记突出显示(每种类型的颜色不同)
- 注意标记可能会覆盖/透明度很有用
- 标记类型
a周围会有衰减(按小时数配置)
我考虑过使用 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 工作,这也感觉不太对 - 特别是如果队列 ID 中的元素数量(时间序列)变得更大。 如果 FacetGrid 是正确的方法,那么类似于以下内容的内容:https://seaborn.pydata.org/examples/timeseries_facets.html 将是第一部分,但标签仍然会丢失。
如何添加标签? 第一部分应该如何完成?
期望结果的示例: https://imgur.com/9J1EcmI,即其中之一
对于每个指标值
示例数据代码
数据集是从下面的代码 sn-p 生成的:
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'])
【问题讨论】:
-
每个度量图中的三行是什么?
-
黑色的?它们应该代表各个时间序列
标签: python matplotlib time-series seaborn highlight