Gantt chart using data in milliseconds

Hello,

I would like to represent the presence of actors on screen at different times using the plotly gantt chart. The timestamps are in seconds, which I would like to represent along the xaxis, and if possible, use for the time information in the hovertext.

So far, I’ve only managed to remove the data information:

fig = ff.create_gantt(df, colors=rgb, index_col='Task', group_tasks=True)
fig['layout']['xaxis']['tickformat'] = '%L'
fig['layout']['xaxis']['tickvals'] = np.arange(0,5400)
fig['layout']['xaxis']['ticktext']  = list(range(len(fig['layout']['xaxis']['tickvals'])))

py.iplot(fig, filename='gantt-test', world_readable=True)

Which produces:

Link to mock data (Google Sheets): https://bit.ly/2CJ5CZH

Hi @barnold,

Welcome to the forums! This sounds like an interesting plot. Could you share a small dataset (made up is fine) so that you’re current example can be reproduced by other folks?

It may end up being easier to do this starting with a standard bar trace rather than starting with the gantt figure factor. They key here is that each bar in a bar trace can have both length and starting position specified. See https://plot.ly/python/bar-charts/#customizing-individual-bar-base.

-Jon

Hi Jon,

Thanks for your reply!
I’ve added a mock dataset, https://bit.ly/2CJ5CZH . I think that the Gantt chart is the simplest option for visualizing this, because of the grouping feature.

-al

Hi @barnold,

Here’s an example of the idea I mentioned above of using a bar chart for this:

import pandas as pd
import plotly.graph_objs as go
from plotly.offline import init_notebook_mode, iplot
init_notebook_mode()

df = pd.read_csv('datasets/movie_timeline.csv')
df['duration'] = df['end'] - df['start']

fig = go.Figure(
    layout = {
        'barmode': 'stack',
        'xaxis': {'automargin': True},
        'yaxis': {'automargin': True}}#, 'categoryorder': 'category ascending'}}
)

for character, character_df in df.groupby('character'):
    fig.add_bar(x=character_df.duration,
                y=character_df.character,
                base=character_df.start,
                orientation='h',
                showlegend=False,
                name=character)
    
iplot(fig)

Hope that helps! And if you’re still interested in trying the gantt approach, I can take a look if you post a full code example of what you have so far.

-Jon

hi @jmmease,

Ah ok, thanks!

Here’s the full code I’m using currently:

import plotly 
import plotly.plotly as py
import plotly.figure_factory as ff
import pandas as pd
import random
import numpy as np

df = pd.read_csv('xray1.csv')
df.columns = ['nconst', 'Task', 'Start', 'Finish']

chars = df['Task'].value_counts()
rgb = []
for c in range(len(df)):
    r = list(np.random.choice(range(256), size=3))
    s2 = ','.join(map(str,r))
    s1 = "rgb("
    s3 = ")"
    rgb.append(s1 + s2 + s3)
    

fig = ff.create_gantt(df, colors=rgb, index_col='Task', group_tasks=True)
fig['layout']['title'] = '(500) Days of Summer- Actors on screen'
fig['layout']['xaxis']['tickformat'] = '%L'
fig['layout']['xaxis']['tickvals'] = np.arange(0,5400)
fig['layout']['xaxis']['ticktext']  = list(range(len(fig['layout']['xaxis']['tickvals'])))

py.iplot(fig, filename='gantt-test')

If you have any input there, I would greatly appreciate it.

-al

Hi @barnold,

You can hide the range buttons by setting the rangeselector property of the xaxis to None and you can give the xaxis a linear (non-date) axis type by setting the type property of the xaxis to linear.

import plotly 
import plotly.figure_factory as ff
import pandas as pd
import random
import numpy as np

df = pd.read_csv('datasets/movie_timeline.csv')
df.columns = ['nconst', 'Task', 'Start', 'Finish']

chars = df['Task'].value_counts()
rgb = []
for c in range(len(df)):
    r = list(np.random.choice(range(256), size=3))
    s2 = ','.join(map(str,r))
    s1 = "rgb("
    s3 = ")"
    rgb.append(s1 + s2 + s3)
    

fig = go.Figure(ff.create_gantt(df, colors=rgb, index_col='Task', group_tasks=True))
fig['layout']['title'] = '(500) Days of Summer- Actors on screen'
fig['layout']['xaxis']['tickformat'] = '%L'
fig['layout']['xaxis']['tickvals'] = np.arange(0,5400)
fig['layout']['xaxis']['ticktext']  = list(range(len(fig['layout']['xaxis']['tickvals'])))
fig.layout.xaxis.rangeselector = None
fig.layout.xaxis.type = 'linear'

Hope that helps you along the way!
-Jon