Tre*_*ney 4 python matplotlib seaborn displot kdeplot
hue组的 aseaborn.kdeplot或seaborn.displotwithkind='kde'赋予不同的值linestyle?
strfor linestyle/ls,它适用于所有hue组。import seaborn as sns
import matplotlib.pyplot as plt
# load sample data
iris = sns.load_dataset("iris")
# convert data to long form
im = iris.melt(id_vars='species')
# axes-level plot works with 1 linestyle
fig = plt.figure(figsize=(6, 5))
p1 = sns.kdeplot(data=im, x='value', hue='variable', fill=True, ls='-.')
# figure-level plot works with 1 linestyle
p2 = sns.displot(kind='kde', data=im, x='value', hue='variable', fill=True, ls='-.')
Run Code Online (Sandbox Code Playgroud)
kdeplotdisplothue组。hue组并迭代每个唯一的组。hue组并迭代每个独特的组。hue_kws不是有效的选项。fill=True要更新的对象位于.collectionsfill=False要更新的对象位于.lineshandles = p.legend_.legendHandles[::-1]提取并反转图例句柄。linestyle它们被反转更新,因为它们的更新顺序与绘图相反._legend,轴级图使用.legend_。python 3.8.12, matplotlib 3.4.3,seaborn 0.11.2kdeplot: 轴级.collections通过或.lines从对象中提取和迭代axes并使用.set_linestylefill=Truefig = plt.figure(figsize=(6, 5))
p = sns.kdeplot(data=im, x='value', hue='variable', fill=True)
lss = [':', '--', '-.', '-']
handles = p.legend_.legendHandles[::-1]
for line, ls, handle in zip(p.collections, lss, handles):
line.set_linestyle(ls)
handle.set_ls(ls)
Run Code Online (Sandbox Code Playgroud)
fill=Falsefig = plt.figure(figsize=(6, 5))
p = sns.kdeplot(data=im, x='value', hue='variable')
lss = [':', '--', '-.', '-']
handles = p.legend_.legendHandles[::-1]
for line, ls, handle in zip(p.lines, lss, handles):
line.set_linestyle(ls)
handle.set_ls(ls)
Run Code Online (Sandbox Code Playgroud)
displot:图形级别handles可以在 中更新for line, ls, handle in zip(ax.collections, lss, handles),但这会将更新应用于每个子图。因此,创建一个单独的循环来仅更新图例handles一次。fill=Trueg = sns.displot(kind='kde', data=im, col='variable', x='value', hue='species', fill=True, common_norm=False, facet_kws={'sharey': False})
axes = g.axes.flat
lss = [':', '--', '-.']
for ax in axes:
for line, ls in zip(ax.collections, lss):
line.set_linestyle(ls)
handles = g._legend.legendHandles[::-1]
for handle, ls in zip(handles, lss):
handle.set_ls(ls)
Run Code Online (Sandbox Code Playgroud)
fill=Falseg = sns.displot(kind='kde', data=im, col='variable', x='value', hue='species', common_norm=False, facet_kws={'sharey': False})
axes = g.axes.flat
lss = [':', '--', '-.']
for ax in axes:
for line, ls in zip(ax.lines, lss):
line.set_linestyle(ls)
handles = g._legend.legendHandles[::-1]
for handle, ls in zip(handles, lss):
handle.set_ls(ls)
Run Code Online (Sandbox Code Playgroud)