Search code examples
pythondatetimeformattingplotly

Plotly - how to format x-axis datetime to a custom format [12AM = 12a]?


I'm trying to change the format of my datetime x-axis ticks to be a specific custom format that matches the rest of my company's formatting.

using fig.update_xaxes(tickformat='%-I %p') I can get to a format that looks like this:

[12AM, 3AM, 6AM, 9AM, 12p, etc...]

But my desired output is this:

[12a, 3a, 6a, 9a, 12p, etc...]

Is there a way to do this simply using the tickformat options, or do I have do some sort of lambda function in addition? If so, what would that look like? Thanks so much for your help.


Solution

  • Your format seams not to be available according to this list. You could format it yourself using ticktext and a formatting function which you probably already have, source here.

    fig.update_layout(
        xaxis = dict(
            tickmode = 'array',
            tickvals = dates_array,
            ticktext = [compute_your_format_from_date(date) for date in dates_array],
        )
    )