Как выделить отрезки графика в matplotlib / seaborn?

#python #matplotlib #time-series #seaborn #highlight

#python #matplotlib #временные ряды #seaborn #выделить

Вопрос:

У меня есть несколько временных рядов и несколько меток. Всякий раз, когда есть доступная метка, я бы хотел выделить временные ряды красным цветом.

существующий график

У меня есть линейная диаграмма, на которой я могу выделить определенные элементы графика, такие как:

введите описание изображения здесь

 for cohort_id in sorted(df.cohort_id.unique()):
    print(cohort_id)
    figsize = (25, 9)
    fig, ax = plt.subplots(figsize=figsize)
    ax = sns.lineplot(x='hour', y='metrik_0', data=df[df.cohort_id == cohort_id], ax=ax)
    ax.xaxis.set_major_locator(aut_locator)
    ax.xaxis.set_major_formatter(aut_formatter)
    
    plt.title(f'cohort_id: {cohort_id}', fontsize=45)
    plt.xlabel('')
    plt.ylabel('metrik_0', fontsize=35)
    
    for index, row in marker_labels.iterrows():
        start = row.start
        end = row.end
        marker_type = row.marker_type
        if marker_type == 'b':
            ax.axvspan(start, end, color='gray', alpha=0.2)
        else:
            ax.axvspan(start, end, color='orange', alpha=0.5)
        
    plt.show()
  

Этот график может быть преобразован в график циклов, выделяющий определенные периоды, такие как:

введите описание изображения здесь

 for cohort_id in sorted(df.cohort_id.unique()):
    print(cohort_id)
    
    figsize = (25, 9)
    fig, ax = plt.subplots(figsize=figsize)
    a1 = sns.lineplot(x=df['hour'].dt.hour, y='metrik_0', hue='device_id', units='dt', style='dt', estimator=None, data=df[(df.cohort_id == cohort_id)], ax=ax)
    handles, labels = a1.get_legend_handles_labels()
    a1.legend(handles=handles[1:], labels=labels[1:], loc='center', bbox_to_anchor=(0.5, -0.25), ncol=6, fontsize=20)

    plt.title(f'cohort_id: {cohort_id}', fontsize=35)
    plt.xlabel('hour of the day', fontsize=35)
    plt.ylabel('metrik_0', fontsize=35)
    plt.show()
  

Но теперь метки больше не могут отображаться.

вопрос

Как я могу повторно добавить метки к графику цикла? Подойдет любой метод. Но до сих пор я думал, что было бы лучше выделить соответствующие временные интервалы красным

генерация данных

Для создания некоторых примеров данных:

 %pylab inline

import pandas as pd
import numpy as np
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)

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 = df.reset_index().rename(columns={'index':'hour'})
df['dt'] = df.hour.dt.date

and labels:

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'])
marker_labels.loc[marker_labels['end'].isnull(), 'end'] =  marker_labels.start   pd.Timedelta(days=1) - pd.Timedelta(seconds=1)
marker_labels
  

Подробный блокнот Jupyter с примерами данных и текущим кодом построения графика можно найти здесь: https://github.com/geoHeil/plotting_tricks

Редактировать

Предполагая, что мы выполняем ЛЕВОЕ соединение для меток временных сегментов:

 merged_res = (df.reset_index()
         .merge(marker_labels, on='cohort_id', how='left')
         .query('start <= hour <= end')
         .set_index('index')
         .reindex(df.index)
      )

merged_res = merged_res.combine_first(df)
merged_res.marker_type = merged_res.marker_type.fillna('no_labels_reported')
  

с помощью кода построения графика:

 for cohort_id in sorted(merged_res.cohort_id.unique()):
    print(cohort_id)
    
    figsize = (25, 9)
    fig, ax = plt.subplots(figsize=figsize)
    a1 = sns.lineplot(x=merged_res['hour'].dt.hour, y='metrik_0', hue='marker_type', units='dt', style='dt', estimator=None, data=merged_res[(merged_res.cohort_id == cohort_id)], ax=ax)
    handles, labels = a1.get_legend_handles_labels()
    a1.legend(handles=handles[1:], labels=labels[1:], loc='center', bbox_to_anchor=(0.5, -0.25), ncol=6, fontsize=20)

    plt.title(f'cohort_id: {cohort_id}', fontsize=35)
    plt.xlabel('hour of the day', fontsize=35)
    plt.ylabel('metrik_0', fontsize=35)
    plt.show()
  

результаты в:

введите описание изображения здесь

Однако:

  • это все еще довольно хаотично
  • отдельные временные ряды устройств агрегируются / усредняются в визуализации

Ответ №1:

Пока, похоже, лучше всего использовать hvplot:

 merged_res['hour_time'] = merged_res['hour'].dt.hour
merged_res.device_id = merged_res.device_id.astype(str)

for cohort_id in sorted(merged_res.cohort_id.unique()):
    print(cohort_id)
    current_plot = merged_res[merged_res.cohort_id == cohort_id].set_index(['hour_time'])[['metrik_0',  'marker_type', 'device_id', 'dt']].hvplot(by=['marker_type'], 
                                                                                                                                                  hover_cols=['dt', 'device_id'], width=width, height=height).opts(active_tools=['box_zoom'])
    display(current_plot)
  

в результате:
введите описание изображения здесь

Поскольку я все еще не полностью удовлетворен — я оставлю его открытым (без ответа), чтобы посмотреть, придумает ли кто-нибудь лучшее решение.

В частности, мне не нравится, что это отображает линии — вероятно, точки были бы лучше. Т.е. Когда что-то меняется с отсутствия метки на наличие метки, временные ряды не рисуются непрерывно (= изменение цвета), а фактически прыгают (= создается новая другая строка. Таким образом, использование точек также было бы обходным решением (но, вероятно, лучше, чем наличие прыгающих линий.

Комментарии:

1. Т.е. line_dash='dotted' Стоит попробовать — но тоже не полностью.