使用Seaborn的带有最小/最大阴影的时间序列图 [英] Timeseries plot with min/max shading using Seaborn

查看:292
本文介绍了使用Seaborn的带有最小/最大阴影的时间序列图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据以下数据创建3线时间序列图,在Week x Overload图中,其中每个群集都是不同的线.

I am trying to create a 3-line time series plot based on the following data , in a Week x Overload graph, where each Cluster is a different line.

我对每对(集群,周)对都有多个观测值(每个atm为5,将有1000个观测值).我希望线上的点是该特定(群集,周)对的平均过载值,而带是其最小/最大值.

I have multiple observations for each (Cluster, Week) pair (5 for each atm, will have 1000). I would like the points on the line to be the average Overload value for that specific (Cluster, Week) pair, and the band be the min/max values of it.

当前使用下面的代码来绘制它,但是我没有得到任何行,因为我不知道使用当前数据帧指定什么单位:

Currently using the following bit of code to plot it, but I'm not getting any lines, as I don't know what unit to specify using the current dataframe:

    ax14 = sns.tsplot(data = long_total_cluster_capacity_overload_df, value = "Overload", time = "Week", condition = "Cluster")

GIST数据

我觉得我仍然需要重塑数据框,但我不知道该怎么做.寻找看起来像这样的最终结果

I have a feeling I still need to re-shape my dataframe, but I have no idea how. Looking for a final results that looks like this

推荐答案

基于

Based off this incredible answer, I was able to create a monkey patch to beautifully do what you are looking for.

import pandas as pd
import seaborn as sns    
import seaborn.timeseries

def _plot_range_band(*args, central_data=None, ci=None, data=None, **kwargs):
    upper = data.max(axis=0)
    lower = data.min(axis=0)
    #import pdb; pdb.set_trace()
    ci = np.asarray((lower, upper))
    kwargs.update({"central_data": central_data, "ci": ci, "data": data})
    seaborn.timeseries._plot_ci_band(*args, **kwargs)

seaborn.timeseries._plot_range_band = _plot_range_band

cluster_overload = pd.read_csv("TSplot.csv", delim_whitespace=True)
cluster_overload['Unit'] = cluster_overload.groupby(['Cluster','Week']).cumcount()

ax = sns.tsplot(time='Week',value="Overload", condition="Cluster", unit="Unit", data=cluster_overload,
               err_style="range_band", n_boot=0)

输出图:

请注意,阴影区域与折线图中的真实最大值和最小值对齐!

Notice that the shaded regions line up with the true maximum and minimums in the line graph!

如果您知道为什么需要unit变量的原因,请告诉我.

If you figure out why the unit variable is required, please let me know.

如果您不希望它们全部出现在同一图形上,则:

If you do not want them all on the same graph then:

import pandas as pd
import seaborn as sns
import seaborn.timeseries


def _plot_range_band(*args, central_data=None, ci=None, data=None, **kwargs):
    upper = data.max(axis=0)
    lower = data.min(axis=0)
    #import pdb; pdb.set_trace()
    ci = np.asarray((lower, upper))
    kwargs.update({"central_data": central_data, "ci": ci, "data": data})
    seaborn.timeseries._plot_ci_band(*args, **kwargs)

seaborn.timeseries._plot_range_band = _plot_range_band

cluster_overload = pd.read_csv("TSplot.csv", delim_whitespace=True)
cluster_overload['subindex'] = cluster_overload.groupby(['Cluster','Week']).cumcount()

def customPlot(*args,**kwargs):
    df = kwargs.pop('data')
    pivoted = df.pivot(index='subindex', columns='Week', values='Overload')
    ax = sns.tsplot(pivoted.values, err_style="range_band", n_boot=0, color=kwargs['color'])

g = sns.FacetGrid(cluster_overload, row="Cluster", sharey=False, hue='Cluster', aspect=3)
g = g.map_dataframe(customPlot, 'Week', 'Overload','subindex')

产生以下内容((如果您认为比例不正确,显然可以使用宽高比))

Which produces the following, (you can obviously play with the aspect ratio if you think the proportions are off)

这篇关于使用Seaborn的带有最小/最大阴影的时间序列图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆