Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change line style in seaborn facet grid

I have a data set 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})

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()

enter image description here

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

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

UPDATE :

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()

enter image description here

like image 844
spore234 Avatar asked Dec 19 '22 07:12

spore234


1 Answers

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 )

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.

To change the xlimits you can use the xlim argument

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

enter image description here

like image 94
ImportanceOfBeingErnest Avatar answered Dec 21 '22 11:12

ImportanceOfBeingErnest