更改seaborn facet网格中的线型 [英] change line style in seaborn facet grid

查看:185
本文介绍了更改seaborn facet网格中的线型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的数据集

import numpy as np; np.random.seed(3)
import pandas as pd
import seaborn.apionly as sns
import matplotlib.pyplot as plt

def get_data(n=266, s=[5,13]):
    val = np.c_[np.random.poisson(lam=s[0], size=n),
                np.random.poisson(lam=s[1], size=n)].T.flatten()
    comp = [s[0]]*n +  [s[1]]*n
    ov = np.random.choice(list("ABC"), size=2*n)
    return pd.DataFrame({"val":val, "overlap":ov, "comp":comp})

data1 = get_data(s=[9,11])
data2 = get_data(s=[9,11])
data3 = get_data(s=[9,11])

#option1 combine
for i, df in enumerate([data1,data2,data3]):
    df["data"] = ["data{}".format(i+1)] * len(df)

data = data1.append(data2)
data = data.append(data3)

bw = 2
a = sns.FacetGrid(data, col="overlap",  hue="comp", row="data")
a = (a.map(sns.kdeplot, "val",bw=bw ))
plt.show()

我希望橙色线(与数据框中的comp = 11对应)为黑色和虚线,怎么办?

I want the orange line (which corresponds to comp=11 in the data frame) to be black and dashed, how can I do that?

我还想控制某些子图的xlim.有可能吗?

I also want to control the xlim for some subplots. Is that possible?

更新:

我需要添加这些方面可以具有不同数量的色相级别,像这样:

I need to add the the facets can have different nummbers of hue levels, like this:

import numpy as np; np.random.seed(3)
import pandas as pd
import seaborn.apionly as sns
import matplotlib.pyplot as plt

def get_data(n=266, s=[5,13]):
    val = np.c_[np.random.poisson(lam=s[0], size=n),
                np.random.poisson(lam=s[1], size=n)].T.flatten()
    comp = [s[0]]*n +  [s[1]]*n
    ov = np.random.choice(list("ABC"), size=2*n)
    return pd.DataFrame({"val":val, "overlap":ov, "comp":comp})

def get_data2(n=266, s=[5,13,3]):
    val = np.c_[np.random.poisson(lam=s[0], size=n),
                np.random.poisson(lam=s[1], size=n),
                np.random.poisson(lam=s[1], size=n)].T.flatten()
    comp = [s[0]]*n +  [s[1]]*n + [s[2]]*n
    ov = np.random.choice(list("ABC"), size=3*n)
    return pd.DataFrame({"val":val, "overlap":ov, "comp":comp})

data1 = get_data(s=[9,11])
data2 = get_data2(s=[7,9,11])
data3 = get_data(s=[9,11])

#option1 combine
for i, df in enumerate([data1,data2,data3]):
    df["data"] = ["data{}".format(i+1)] * len(df)

data = data1.append(data2)
data = data.append(data3)

bw = 2
a = sns.FacetGrid(data, col="overlap",  hue="comp", row="data")
a = (a.map(sns.kdeplot, "val",bw=bw ))
plt.show()

推荐答案

您可以使用FacetGridhue_kws参数来更改映射图的颜色或线型.

You can use the hue_kws argument to FacetGrid for changing the color or linestyle of the mapped plot.

d = {'color': ['C0', 'k'], "ls" : ["-","--"]}
g = sns.FacetGrid(data, col="overlap",  hue="comp", row="data",hue_kws=d )

如果使用更多的色相级别,则需要提供更多的颜色和线条样式,例如d = {'color': ['C0', 'k', "crimson"], "ls" : ["-","--", "-."]}表示3种色调.

If more hue levels are used, more colors and linestyles need to be provided, e.g. d = {'color': ['C0', 'k', "crimson"], "ls" : ["-","--", "-."]} for 3 hue levels.

要更改xlimit,可以使用xlim参数

To change the xlimits you can use the xlim argument

g = sns.FacetGrid(..., xlim=(-10,40) )

这篇关于更改seaborn facet网格中的线型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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