使用 Seaborn 绘制具有最小/最大着色的时间序列图

2023-11-21

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

我对每个(集群、周)对都有多个观察结果(每个 atm 5 个,将有 1000 个)。我希望线上的点是该特定(集群、周)对的平均过载值,而范围是它的最小/最大值。

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

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

胃肠道间质瘤数据

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 enter image description here


基于这个令人难以置信的答案,我能够创建一个猴子补丁来完美地完成您正在寻找的事情。

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)

Output Graph: enter image description here

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

如果你明白为什么unit需要变量,请告诉我。


如果您不希望它们全部出现在同一个图表上,那么:

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) enter image description here

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Seaborn 绘制具有最小/最大着色的时间序列图 的相关文章

随机推荐