python - 如何为 kdeplot/displot 中的每个色调组设置不同的线型

标签 python matplotlib seaborn displot kdeplot

  • seaborn.kdeplot 的每个 hue 组如何? , 或 seaborn.displotkind='kde' 被赋予不同的 linestyle
    • 轴级和图形级选项都将接受 linestyle/lsstr,它适用于所有 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='-.')
  • kdeplot

enter image description here

  • displot

enter image description here

评论问题

最佳答案

  • 使用 fill=True,要更新的对象位于 .collections
  • 使用 fill=False,要更新的对象位于 .lines
  • 更新图例非常简单:
    • handles = p.legend_.legendHandles[::-1] 提取并反转图例句柄。它们被反转来更新,因为它们的顺序与情节 linestyle 的更新顺序相反
    • 请注意,图级图使用 ._legend 提取图例,轴级图使用 .legend_
  • python 3.8.12matplotlib 3.4.3seaborn 0.11.2 中测试

kdeplot:轴级

  • axes 对象中提取并遍历 .collections.lines 并使用 .set_linestyle<

fill=True

fig = 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)

enter image description here

fill=False

fig = 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)

enter image description here

displot:图形级

  • 类似于轴级图,但每个轴都必须迭代
  • 图例 handles 可以在 for line, ls, handle in zip(ax.collections, lss, handles) 中更新,但这会将更新应用于每个子图.因此,创建了一个单独的循环来仅更新图例 handles 一次。

fill=True

g = 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)

enter image description here

fill=False

g = 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)

enter image description here

关于python - 如何为 kdeplot/displot 中的每个色调组设置不同的线型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70089199/

相关文章:

python - 为什么 torch.from_numpy 需要不同的字节顺序而 matplotlib 不需要?

python for 循环子图

python - 如何在 Seaborn/Matplotlib 中将所有边缘颜色设置为无?

python - Numpy 查找大于前 n 个元素的元素

python - 如何获得 Pyramid 中被调用 View 的权限?

python - 在同一图形上绘制多个可能轴的方法

python - 以一个索引作为 Y 轴,另一个作为 X 轴绘制 pandas 多索引 DataFrame

python - 多指数作图

python - 创建一个允许用户选择日期过滤的 View /模板,其中包括子对象的总计?

python - 如何继承 defaultdict 并在子类方法中使用它的复制方法?