gpt4 book ai didi

python - 更改 seaborn 面网格中的线条样式

转载 作者:行者123 更新时间:2023-11-28 20:16:08 26 4
gpt4 key购买 nike

我有这样的数据集

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

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

我还想控制一些子图的 xlim。这可能吗?

更新:

我需要添加分面可以有不同数量的色调级别,如下所示:

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

最佳答案

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

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

如果使用更多色调级别,则需要提供更多颜色和线型,例如d = {'color': ['C0', 'k', "crimson"], "ls": ["-","--", "-."]} for 3色调水平。

要更改 xlimits,您可以使用 xlim 参数

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

enter image description here

关于python - 更改 seaborn 面网格中的线条样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44261803/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com