Random date tick labels

Sorry for resurrecting an old post, but I never got any replies (I can’t believe this doesn’t bother anyone else).

When plotting a time series (date/time axis), plotly seems to just randomly choose the first tick value and the tick spacing.

Take this example, no where in the code can you tell it where the first tick label should be and what the spacing is

Surely there must be a way to make this more readable to the human eye?

14 day spacing is not a natural, easy to follow spacing, nor are the tick labels themselves.

What I am after is a sense of control to make the plot more readable, i.e. use the first date of the month, or some other defined period (weekly, monthly, annual)

See this example, which is explained in most detail in Ref. In this example, the month unit is ‘M1’. To make it a two-month unit, use ‘M2’; for a one-week unit, use ‘D7’. See this for notation in plotly.

fig.update_xaxes(
    rangeslider_visible=True,
    dtick="M2",
    tickformat="%d.%b\n%Y")

2 Likes

Thanks r-beginners. I should have been more clear. I was following this example with tickformatstops;

fig.update_xaxes(
    rangeslider_visible=True,
    tickformatstops = [
        dict(dtickrange=[None, 1000], value="%H:%M:%S.%L ms"),
        dict(dtickrange=[1000, 60000], value="%H:%M:%S s"),
        dict(dtickrange=[60000, 3600000], value="%H:%M m"),
        dict(dtickrange=[3600000, 86400000], value="%H:%M h"),
        dict(dtickrange=[86400000, 604800000], value="%e. %b d"),
        dict(dtickrange=[604800000, "M1"], value="%e. %b w"),
        dict(dtickrange=["M1", "M12"], value="%b '%y M"),
        dict(dtickrange=["M12", None], value="%Y Y")
    ]
)


In the screenshot I gave, it’s at a zoom level where dtickrange=[604800000, "M1"], value="%e. %b w" applies. But it doesn’t say what the leading tick value is or what the spacing is.

dict(dtickrange=[604800000, "M1"], value="%e. %b w")

This represents more than 7 days and less than 1 month. (604800000/86400000=7 days) %e would be the day of the month in decimal, filled with spaces. w would be added as a string to be distinguished.
The numbers mean 1000 milliseconds, 1000 * 60 seconds, 1000 * 60 * 60 minutes, and 1000 * 60 * 60 * 24 hours.

Thanks again r-beginners. I understand those dtickrange brackets control the tick label format, but it doesn’t explain how plotly chooses the first tick label and it’s subsequent example.

It seems that plotly just chooses around 5 (give or take) tick labels at any zoom level. That could mean 1, 2 or 3 monthly spacing, 14, 7, 2 day spacing or just whatever it wants. The spacing doesn’t seem to have anything to do with tickformatstops, only the tick label format.

This is the control I’m trying to understand.

Because they are dates and not numbers, it’s just not that easy for a human to quickly understand spacings, given humans are looking for specific reference points, i.e.

  • Quarterly - every quarter starting on 1 Jan, 1 Apr, 1 Jul, 1 Oct (not other months). “What happened at the start of Q2”
  • Monthly spacing - every calendar month starting on the 1st.
  • Weekly spacing - every week starting on a Sunday “What happened in the second week of September”
  • Daily spacing - label at the start of every day. “What happened at 5pm of the 12th”

Is there a way to add some order to date tick labels?

I cannot answer your question about the logic of the first tick and the subsequent ticks. Perhaps an expert in this community can answer that for you.

how plotly chooses the first tick label and it’s subsequent example.

You can specify a start and end date with xaxis_range=[start_date,end_date]. You can also specify the start date with tick0=start_date.