GuiGui
GuiGui

Reputation: 133

How to plot a Gantt chart using timesteps and not dates using plotly

So I found this code online that would make a Gantt chart:

import plotly.express as px
import pandas as pd

df = pd.DataFrame([
    dict(Task="Job A", Start='2009-01-01', Finish='2009-02-28', Resource="Alex"),
    dict(Task="Job B", Start='2009-03-05', Finish='2009-04-15', Resource="Alex"),
    dict(Task="Job C", Start='2009-02-20', Finish='2009-05-30', Resource="Max")
])

fig = px.timeline(df, x_start="Start", x_end="Finish", y="Task", color="Resource")
fig.update_yaxes(autorange="reversed")
fig.show()

However, I wish to not use date and times but rather use timesteps, starting from 0 nanoseconds and so forth, what I would want is something that would look like this:

import plotly.express as px
import pandas as pd

df = pd.DataFrame([
    dict(Task="Job A", Start=0, Finish=10, Resource="Alex"),
    dict(Task="Job B", Start=12, Finish=24, Resource="Alex"),
    dict(Task="Job C", Start=5, Finish=20, Resource="Max")
])

fig = px.timeline(df, x_start="Start", x_end="Finish", y="Task", color="Resource")
fig.update_yaxes(autorange="reversed")
fig.show()

Is this something that would be possible using plotly or does it only use Date and time? I want to use plotly because it gives a very clean graph.

I've looked in the plotly express documentation but found nothing to convert and use timesteps.

Upvotes: 4

Views: 6291

Answers (2)

Mujtaba Mateen
Mujtaba Mateen

Reputation: 202

Plotly Gantt charts only work with dates, but a possible workaround is to add the date 1970-01-01 to the beginning of all of the times, then display the times (in this case, seconds) without displaying the date in your plot.

import plotly.express as px
import pandas as pd
df = pd.DataFrame([
dict(Task="Job A", Start='1970-01-01 00:00:00', Finish='1970-01-01 00:00:10', Resource="Alex"),
dict(Task="Job B", Start='1970-01-01 00:00:12', Finish='1970-01-01 00:00:24', Resource="Alex"),
dict(Task="Job C", Start='1970-01-01 00:00:05', Finish='1970-01-01 00:00:20', Resource="Max")
])
fig = px.timeline(df, x_start="Start", x_end="Finish", y="Task", 
color="Resource")

fig.update_layout(xaxis=dict(
                  title='Timestamp', 
                  tickformat = '%S'))
fig.update_yaxes(autorange="reversed")
fig.show()

Upvotes: 1

Redox
Redox

Reputation: 10017

You can do this using plotly.figure_factory gantt chart and forcing the x-axis to show numbers. There are few examples here, if you need to know more about this. The code output is as shown below.

import pandas as pd
import plotly.figure_factory as ff

df = pd.DataFrame([
    dict(Task="Job A", Start=0, Finish=10, Resource="Alex"),
    dict(Task="Job B", Start=12, Finish=24, Resource="Alex"),
    dict(Task="Job C", Start=5, Finish=20, Resource="Max")
])

fig = ff.create_gantt(df, index_col = 'Resource',  bar_width = 0.4, show_colorbar=True)
fig.update_layout(xaxis_type='linear', autosize=False, width=800, height=400)
fig.show()

Plot

enter image description here

Upvotes: 5

Related Questions