Search code examples
pythonseabornhistogramdisplot

How to change the histogram bar width in displot


I'm using seaborn's displot to get stacked bar charts. Here is my code:

g = sns.displot(data=df, x='collection_year', hue='WB_Income group', multiple='stack',col="ena_species",col_wrap=6,facet_kws={'sharey': False, 'sharex': False})

and here is the output enter image description here

How can I increase the width of the bars?


Solution

    • In this case, the x-axis is continuous, as is typical of a histogram.
    • This issue can be resolved by setting discrete=True or setting bins= to match the number of unique values in 'year', which results in the x-ticks corresponding only to the values passed to the independent axis.
    • Tested in python 3.10, pandas 1.4.3, matplotlib 3.5.1, seaborn 0.11.2

    Imports & Sample DataFrame

    import pandas as pd
    import numpy as np
    import seaborn as sns
    
    # create sample data
    np.random.seed(2022)
    rows = 10000
    data = {'year': np.random.choice(list(range(1985, 2025)), size=rows),
            'income': np.random.choice(['high', 'upper middle', 'lower middle', 'low'], size=rows),
            'species': np.random.choice(list(string.ascii_lowercase[:14]), size=rows)}
    df = pd.DataFrame(data)
    
    # display(df.head())
       year        income species
    0  2013  upper middle       m
    1  2009  lower middle       g
    2  2003  lower middle       h
    3  2009  upper middle       g
    4  2001  lower middle       m
    
    • The example plot, with large spaces between each bar, was only reproducible by setting bins=160, which is 4x the number of years from 1985 - 2025.
    • There may be something in the 'year' data that's not apparent in the OP.
    g = sns.displot(data=df, x='year', hue='income', multiple='stack', col="species", col_wrap=6, bins=160, facet_kws={'sharey': False, 'sharex': False})
    

    enter image description here

    • The issue can be resolved by setting bins to match the number of years in the data.
    g = sns.displot(data=df, x='year', hue='income', multiple='stack', col="species", col_wrap=6, bins=40, facet_kws={'sharey': False, 'sharex': False})
    
    • The issue can also be resolved by removing bins, and setting discrete=True.
    g = sns.displot(data=df, x='year', hue='income', multiple='stack', col="species", col_wrap=6, discrete=True, facet_kws={'sharey': False, 'sharex': False})
    

    enter image description here

    • Setting bins to a number less than the number of unique values in 'year' will produce a histogram where each bar encompasses multiple years.
    g = sns.displot(data=df, x='year', hue='income', multiple='stack', col="species", col_wrap=6, bins=20, facet_kws={'sharey': False, 'sharex': False})
    

    enter image description here