Stacked Pivot Charts#

Plotting charts where the input data is aggregated by a categorical column can be pivoted using pivot_column_name in the series function.

When pivoting and wanting to stack each series per point on the x-axis:

  • Each category can be manually plotted as a series by specifying the category_name parameter and the pivot_column_name to use for the series

  • The column_name should remain as the numerical column to plot. The data will for the series will be filtered to the specific category_name automatically

  • The is_stacked parameter can control if the series are stacked vertically or kept side by side (default)

  • When is_stacked=True, Axis min/max are automatically calculated as the sum of all series on the specified y1 or y2 axis

  • The order of the series in the legend and in the graph is based on the order of elements in chart_series parameter.



import pandas as pd

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


fig = chart(
    pdf,
    x_axis=axis(column_name="fiscal_qy", label="Fiscal Quarter"),
    y1_axis=axis(label="Downloads", axis_type="number", axis_min=0),
    chart_series=[
        series(
            column_name="ios_dl_idx",
            category_name="UCAN",
            color="light-blue",
            mode="bar",
            is_stacked=True,
            pivot_column_name="region",
        ),
        series(
            column_name="ios_dl_idx",
            category_name="China",
            color="light-grey",
            mode="bar",
            is_stacked=True,
            pivot_column_name="region",
        ),
        series(
            column_name="ios_dl_idx",
            category_name="India",
            color="orange",
            mode="bar",
            is_stacked=True,
            pivot_column_name="region",
        ),
    ],
)

fig

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

Gallery generated by Sphinx-Gallery