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})
How can I increase the width of the bars?
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.python 3.10
, pandas 1.4.3
, matplotlib 3.5.1
, seaborn 0.11.2
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
bins=160
, which is 4x the number of years from 1985 - 2025.'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})
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})
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})
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})