无法更改 Seaborn.FacetGrid 中的线条样式

hsv*_*var 2 python plot seaborn

我正在尝试创建一个带有完整和虚线的 FacetGrid,就像在这个评论中一样。根据注释中的代码和FacetGrid 文档,这应该可以工作,但是,我只得到完整的行,没有破折号。

有人可以帮我吗?

最小。工作示例:

import matplotlib
import pandas as pd
import seaborn as sns

# toy data
x = [i for i in range(10)]*3
y = [0.5*i for i in range(10)]
y.extend([0.7*i for i in range(10)])
y.extend([0.3*i for i in range(10)])
mode = ["A" for i in range(10)]
mode.extend(["B" for i in range(10)])
mode.extend(["C" for i in range(10)])
method = ["X" for i in range(5)]
method.extend(["Y" for i in range(5)])
method = method*3
df = pd.DataFrame({'x' : x, 'y' : y, 'mode' : mode, 'method' : method})

sns.set_context("paper")
sns.set(style="whitegrid")
blue = matplotlib.colors.hex2color('#5862f4')
pink = matplotlib.colors.hex2color('#e059c3')


kw = {'color': [pink, pink, blue], 'linestyle' : ["-","--","-"]}
p = sns.FacetGrid(df, col='method', hue='mode', sharey='row', margin_titles=True, hue_kws=kw)
p.map(sns.lineplot, 'x', 'y')
p.axes[0,0].set_xlim(0,10)
p.add_legend()

plt.savefig("test.png", bbox_inches='tight')
Run Code Online (Sandbox Code Playgroud)

结果

Imp*_*est 7

Seaborn 会lineplot覆盖线型,例如将其与其style参数一起使用。在这里,您似乎不想使用style. 而且,似乎根本没有理由使用lineplot。因此,正常plt.plot()将工作得很好。

kw = {'color': [pink, pink, blue], 'linestyle' : ["-","--","-"]}
g = sns.FacetGrid(df, col='method', hue='mode', sharey='row', margin_titles=True, hue_kws=kw)
g.map(plt.plot, 'x', 'y')
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

为了完整起见,这里是如何使用style参数 for lineplotwith a FacetGrid

g = sns.FacetGrid(df, col='method', sharey='row', margin_titles=True)
g.map_dataframe(sns.lineplot, 'x', 'y', style="mode", style_order=list("ABC"))
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

注意,为了保证"mode"列的items到样式的映射一致,需要设置样式顺序。