tidus95
tidus95

Reputation: 359

plotly gantt chart with colorbar

I am producing a gantt chart like here https://plot.ly/python/gantt/#index-by-numeric-variable However, my numeric variable I want to use as index is a positive number much greater than 100. When I use the example code, the colorbar limits to [0,100], resulting in the bars being all in the color of 100. Is there a way to lift the maximum value in a plotly gantt chart created using the code like in the example?

I want the color to be "proportional" to the value of the index.

This is the example code:

import plotly.figure_factory as ff

df = [dict(Task="Job A", Start='2009-01-01', Finish='2009-02-28', Complete=10),
      dict(Task="Job B", Start='2008-12-05', Finish='2009-04-15', Complete=60),
      dict(Task="Job C", Start='2009-02-20', Finish='2009-05-30', Complete=95)]

fig = ff.create_gantt(df, colors='Viridis', index_col='Complete', show_colorbar=True)
fig.show()

To be clear: in my case the variable Complete can reach values as high as 700000

Upvotes: 0

Views: 880

Answers (1)

RubenB
RubenB

Reputation: 525

According to the documentation, it should scale when a numeric index is given. However, looking at the code, it seems the min/max values have been hard coded to 0 and 100. See code line 285 and 286.

One workaround would be to scale your index between 0-100 and manually set the labels of the colorbar to the original min/max:

import warnings
warnings.filterwarnings('ignore', category=FutureWarning)  # plotly returns a FutureWarning due to using .ix

from sklearn import preprocessing
import plotly.figure_factory as ff

# create a dataframe for easier processing wrt the scaled index
df = pd.DataFrame([dict(Task="Job A", Start='2009-01-01', Finish='2009-02-28', Complete=10),
      dict(Task="Job B", Start='2008-12-05', Finish='2009-04-15', Complete=60),
      dict(Task="Job C", Start='2009-02-20', Finish='2009-05-30', Complete=995)])

# scale index (0-100) and add as new column
min_max_scaler = preprocessing.MinMaxScaler()
index_scaled = min_max_scaler.fit_transform(df.Complete.values.reshape(-1,1))
df['index_scaled'] = index_scaled * 100

# create figure based on scaled index
fig = ff.create_gantt(df, index_col='index_scaled', colors='Viridis', show_colorbar=True)

# set scale of color bar
fig.data[-1]['marker']['cmin'] = df.Complete.min()
fig.data[-1]['marker']['cmax'] = df.Complete.max()
fig.data[-2]['marker']['cmin'] = df.Complete.min()
fig.data[-2]['marker']['cmax'] = df.Complete.max()

# Due to indexing on the scaled value, the tooltip of the bars shows this values instead of its original.
# You could iterate over them and adjust, but you'll need to match the sorting of fig.data to the sorting of the dataframe.
# fig.data[0].name = original_value

fig.show()

Note, as mentioned in comments above, you might need to take care of the tooltip of the bars (showing the indexed value). These can also be manually set, but you'll need to match the ordering of the fig.data to the ordering of your original dataframe.

Upvotes: 1

Related Questions