本文介绍了如何向 seaborn FacetGrid 添加附加图并指定颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法创建一个 Seaborn 线图,所有线都是灰色的,平均值为红线?我正在尝试使用 relplot 执行此操作,但我不知道如何将均值与数据分开(而且似乎未绘制均值?).

Is there a way to create a Seaborn line plot with all the lines gray and the mean as a red line? I'm trying to do this with relplot but I don't know how to separate the mean from the data (and it appears the mean isn't being plotted?).

制作可重复的数据框

np.random.seed(1)
n1 = 100
n2 = 10
idx = np.arange(0,n1*2)
x, y, cat, id2 = [], [], [], []

x1 = list(np.random.uniform(-10,10,n2))
for i in idx:
    x.extend(x1)
    y.extend(list(np.random.normal(loc=0, scale=0.5, size=n2)))
    cat.extend(['A', 'B'][i > n1])
    id2.append(idx[i])

id2 = id2 * n2
id2.sort()
df1 = pd.DataFrame(list(zip(id2, x, y, cat)),
                  columns =['id2', 'x', 'y', 'cat']
                 )

绘图尝试

g = sns.relplot(
    data=df1, x='x', y='y', hue='id2',
    col='cat', kind='line',
    palette='Greys',
    facet_kws=dict(sharey=False,
                   sharex=False
                  ),
    legend=False
)

推荐答案

我想你希望 units 在对 relplot 的调用中再添加一层 lineplot 使用 map :

I think you want units in the call to relplot and then add a layer of lineplot using map:

import seaborn as sns
import pandas as pd

fm = sns.load_dataset('fmri').query("event == 'stim'")
g = sns.relplot(
    data=fm, kind='line',
    col='region', x='timepoint', y='signal', units='subject',
    estimator=None, color='.7'
)
g.data = fm  # Hack needed to work around bug on v0.11, fixed in v0.12.dev
g.map(sns.lineplot, 'timepoint', 'signal', color='r', ci=None, lw=3)

这篇关于如何向 seaborn FacetGrid 添加附加图并指定颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 17:14