Clustered Bar Chart#

Bar charts can be clustered by a categorical column while plotting values on a time-series for each “category”. This can be created by setting mode=clustered_bar in a series of the chart.

  • Only 1 series can be plotted and it must set a pivot_column_name with include_all_categories=True.

  • The pivot column name is used to define each bar within a cluster. Typically, this will be a date-type column. These values will be stored in the customdata property of the trace.

  • category_sort_column_name can define a sort order for bars within each cluster based on the values of that column.

  • The clusters will share the same y-axis.

  • It is recommended that a custom hovertemplate is used depending on the nature of chart to improve the hover experience for users. This can be provided to extra_options.



import pandas as pd

from visualization_toolkit.helpers.plotly import chart, axis, series
from visualization_toolkit.helpers.plotly.theme import ATLAS_THEME


fig = chart(
    pdf,
    x_axis=axis(column_name="vendor", axis_type="category"),
    y1_axis=axis(label="Spend", axis_type="currency"),
    chart_series=[
        series(
            column_name="spend",
            label="Spend",
            mode="clustered_bar",
            pivot_column_name="month",
            category_sort_column_name="month",
            include_all_categories=True,
            extra_options={
                "hovertemplate": (
                    """
                    <b>%{x}</b><br><span style='color: #9E9E9E;'>Month: </span>%{customdata[0]|%b %Y}<br><span style='color: #9E9E9E;'>Spend: </span>%{y}<br><extra></extra>
                    """
                ).strip(),
            },
        ),
    ],
    theme=ATLAS_THEME,
)

fig

Total running time of the script: (0 minutes 0.078 seconds)

Gallery generated by Sphinx-Gallery