0

I've looked-up answers on all blogs in vain ... I'm simply looking for a way to show datetimes with hour precision on my graphs. Here's an example of what my code generates :

My 'Start Date' & 'End Date' are normally on the following format 'mm-dd-yyyy H-M-S' however it won't show the hour precision until I zoom-in enter image description here

I know the question sounds stupid, but if you can point me in the right direction I would be grateful

Edit :

enter image description here

Keyser Soze
  • 262
  • 3
  • 11
  • Does the original data have time data? That and the data and code that the graph is based on should be shown. – r-beginners May 28 '21 at 12:40
  • Indeed, my original data does have time of course. Check the photo I added in Edit, once I zoom in they start showing me the date & time ... But when I auto-scale to see the whole interval they start clipping time from it even though it's not related to the length or something – Keyser Soze May 28 '21 at 14:51
  • For the X-Axis to be scaled the way that you want, you can refer to this: https://plotly.com/python/tick-formatting/#using-tickformat-attribute--datetime and to get the exact formatting that you want, you can refer to this: https://github.com/d3/d3-time-format/blob/master/README.md – AS11 May 28 '21 at 15:53

1 Answers1

2

It is a case of setting xaxis_tickformat. without this sample below will behave as you described, no hour precision

import pandas as pd
import numpy as np
import plotly.express as px

# for hour slots randonly assign a task
df = pd.DataFrame({"Start": pd.date_range("1-jan-2021", periods=10**3, freq="4H")}).assign(
    End=lambda d: d.Start + pd.Timedelta(hours=1),
    Task=np.random.choice([f"Task {i}" for i in range(2)], 10**3, p=(0.8, 0.2)),
)

# compress consequetive rows
df = df.groupby(df.Task.ne(df.Task.shift()).cumsum()).agg(
    {"Start": "min", "End": "max", "Task": "first"})

fig = px.timeline(df, x_start="Start", x_end="End", y="Task", color="Task")

fig.update_layout(xaxis_tickformat = '%Y-%m-%d %H:%M')

change just hover template not overall axis

fig = px.timeline(df, x_start="Start", x_end="End", y="Task", color="Task")
print(fig.data[0].hovertemplate) # just FYI so can see what default template is
fig.update_traces(hovertemplate="Task=%{y}<br>Start=%{base|%Y-%m-%d %H:%M}<br>End=%{x|%Y-%m-%d %H:%M}<extra></extra>")

Rob Raymond
  • 29,118
  • 3
  • 14
  • 30
  • I truly appreciate responses like these. It is brief, simple & constructive. Thanks a lot Rob for your time ! – Keyser Soze May 31 '21 at 14:07
  • Can't help but notice that your method applies the same format for the plotted data & the x axis as well. Wouldn't there be a way to keep the x axis intact and modify the format of the data only ? I tried modifying the dataframe (that contains the plotted data) and it was in vain – Keyser Soze May 31 '21 at 14:09
  • it's really just the same techniques... have updated how to just change the **hover** text – Rob Raymond May 31 '21 at 15:27