matplotlibseabornhistplot

Controlling Seaborn histplot() lines


Is it possible to change seaborn.histplot() bar lines? They are extremely thin...I tried adding lw = 1.5 or changing edgecolor (since Rectangle gets called) but nothing seems to work. Interestingly, setting lw = 0, Fill=True makes the lines disappear

import seaborn as sns
from matplotlib import pyplot as plt
plt.figure()
sns.histplot(data1, bins=100, kde=True, stat='density', color='blue', alpha=0.5, line_kws={'lw': 1.5}, fill=False, lw=1.5)
sns.histplot(data2, bins=100, kde=True, stat='density', color='red', alpha=0.5, line_kws={'lw': 1.5}, fill=False, lw=1.5)

Unfortunately an error occurs when I try to add a picture, i'll try to add it in the comments


Solution

  • sns.histplot's line_kws are meant to control the (optional) kde line. To change the lines of the bars, the documentation mentions kwarg: all parameters that aren't used by sns.histplot are sent to matplotlib.axes.Axes.bar() (in the default case where bars are used as the element to represent the histogram). matplotlib.axes.Axes.bar() accepts a parameter linewidth= to change the line width. For some strange reason, lw= is ignored.

    Note that fill= is ignored with the default element='bars'; it is meant to optionally fill below the line with element='step' or element='poly'.

    Here is a reproducible example, tested with seaborn 0.13.2:

    import seaborn as sns
    import numpy as np
    
    sns.histplot(np.random.normal(0.01, 1, 1000).cumsum(),
                 linewidth=2, edgecolor='crimson', color='steelblue',
                 kde=True, line_kws={'lw': 3, 'ls': ':'})
    

    sns.histplot with custom linewidth