Home > Software engineering >  How to set a different linestyle for each hue group in a kdeplot / displot
How to set a different linestyle for each hue group in a kdeplot / displot

Time:11-27

  • How can each hue group of a enter image description here

    • displot

    enter image description here

    Reviewed Questions

    • 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: figure-level

      • Similar to the axes-level plot, but each axes must be iterated through
      • The legend handles could be updated in for line, ls, handle in zip(ax.collections, lss, handles), but that applies the update for each subplot. Therefore, a separate loop is created to update the legend handles only once.

      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

  • Related