Numerical axis formatting and intervals#

Numerical axes can be formatted with the tick_interval and tick_format arguments for axis.

  • axis_type should be set to number, percent, or currency.

  • Intervals should be of the same data type as the series on the axis. In most cases this will be a float or and int.

  • Tick formats should be specified as a numerical D3 format (Docs). This is the formatting system Plotly uses and is very similar to the formatting options available in python f-strings.

  • In the example below for the y1_axis:
    • A currency format ($,0.2f) is used. This adds the USD currency prefix, thousands separator, decimal separator, and 2 fixed decimal places to the ticks on the axis.

    • The tick interval is set to 25,000 and thus each tick is separated by that amount

  • In the example below for the y2_axis:
    • A percent format (0.1%) is used. Note that the data values are automatically multipled by 100, with 1 fixed decimal place and a % symbol as a suffix.

    • The tick interval is set to 0.05, to denote 5% steps on the axis



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="Sales",
        axis_type="currency",
        tick_format="$,.2f",
        tick_interval=25000,
        axis_max=250000,
    ),
    y2_axis=axis(
        label="Growth Rate",
        axis_type="percent",
        tick_format=".1%",
        tick_interval=0.05,
        axis_max=0.2,
        axis_min=0.2,
    ),
    chart_series=[
        series(
            column_name="ucan",
            label="UCAN",
            color="light-blue",
            mode="bar",
            is_stacked=True,
        ),
        series(
            column_name="china",
            label="China",
            color="light-grey",
            mode="bar",
            is_stacked=True,
        ),
        series(
            column_name="yy",
            label="Y/Y Growth Rate",
            color="black",
            location="y2",
            shape="dash",
        ),
    ],
)

fig

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

Gallery generated by Sphinx-Gallery