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()
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()
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) )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With