Stacked Bar Chart#

A stacked bar chart showing the total count of penguins per species and how they break down by sex. This example uses the penguins dataset.

import hvplot.pandas  # noqa

df = hvplot.sampledata.penguins('pandas')
grouped = df.groupby(['species', 'sex'], observed=True).size().reset_index(name='count')

grouped.hvplot.bar(
    x='species',
    y='count',
    by='sex',
    stacked=True,
    title='Stacked Bar Chart (Bokeh)',
    xlabel='Species',
    ylabel='Count',
)
import hvplot.pandas  # noqa
hvplot.extension('matplotlib')

df = hvplot.sampledata.penguins('pandas')
grouped = df.groupby(['species', 'sex'], observed=True).size().reset_index(name='count')

grouped.hvplot.bar(
    x='species',
    y='count',
    by='sex',
    stacked=True,
    title='Stacked Bar Chart (Matplotlib)',
    xlabel='Species',
    ylabel='Count',
)
grouped
species sex count
0 Adelie female 73
1 Adelie male 73
2 Chinstrap female 34
3 Chinstrap male 34
4 Gentoo female 58
5 Gentoo male 61
This web page was generated from a Jupyter notebook and not all interactivity will work on this website.