Black Lives Matter. Please consider donating to Black Girls Code today.

How to remove hour ticks and save scaling of timeseries in scatter plot?

I have data for every day. By default plot shows as expected - if there are many days it scales axis and displays ticks through some days depended on scale.


After zoom in plot shows ticks for every day which is also expected.

But after more zoom in plot shows ticks for hours and this is not expected because I don’t have data for hours

So I want to save scaling but not scale axis for more than one day. I have tried set dtick=86400000 value but it turns off scaling at all. And on many days plot looks messy. Same for tickvals+ticktext properties

Is there any chance to do that?

Hi @cooperok,

I don’t think there’s a way to do this directly using layout options. If you’re working in the Jupyter Notebook you could use an on_change callback on a FigureWidget to update dtick yourself on zoom.

For example:

import plotly.graph_objs as go
import pandas as pd

df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv")

data = [go.Scatter(
          x=df.Date,
          y=df['AAPL.Close'])]

fig = go.FigureWidget(data=data)

sec_per_day = 86400000
def change_xaxis(xaxis, xrange):
    delta = pd.to_datetime(xrange).to_series().diff()[1]
    if delta < pd.Timedelta('20 day'):
        xaxis.dtick = sec_per_day
    elif delta < pd.Timedelta('60 day'):
        xaxis.dtick = sec_per_day * 7
    elif delta < pd.Timedelta('365 day'):
        xaxis.dtick = 'M1'
    else:
        xaxis.dtick = 'M3'
fig.layout.xaxis.on_change(change_xaxis, 'range')

fig

Hope that helps!
-Jon

Thank you for reply. Unfortunately I use plotly.python for generating html which can be used anywhere. So this won’t help me