Hi there!
So Iāve created a gantt chart with Plotlyās Dash and Iād be quite happy, if it werenāt for the x axis ticks.
Hereās the chart:
Nice, but for the x axis ticks. Iād like to customize them to fit my needs:
-
Iād like them to start on a monday (04 Jan), instead of a sunday (10 Jan) and continue week-wise as they already do
-
Instead of ā04 Janā, the tick labels should be something like this: āKW: 01 ('21)ā. Here, KW is supposed to mean calendar week.
-
I donāt want to show every tick label. Itād be nice if the chart only shows every first KW of a year and every fifth
So, I was looking for a way to do this and found in Plotly Layout References (xaxis-tickvals) that thereās supposed to be a smooth way to do so. I just need a list of tickvals for the locations in the chart and a list of ticktext for the labels and need to set tickmode to āarrayā.
As my code is too long, Iāll just provide a little example. Letās say this is my code:
import plotly.figure_factory as ff
import datetime
df = [dict(Task="Job A", Start='2021-01-08', Finish='2021-01-29'),
dict(Task="Job B", Start='2021-01-15', Finish='2021-02-12'),
dict(Task="Job C", Start='2021-01-29', Finish='2021-02-19'),
dict(Task="Job D", Start='2021-02-05', Finish='2021-02-26')]
my_figure = ff.create_gantt(df, showgrid_x=True, showgrid_y=True)
weeks_ticktext = ['KW: 01 (\'21)', '', '', '', 'KW: 05 (\'21)', '', '', '', '']
weeks_tickvals = [datetime.datetime(2021, 1, 4, 0, 0), datetime.datetime(2021, 1, 11, 0, 0),
datetime.datetime(2021, 1, 18, 0, 0), datetime.datetime(2021, 1, 25, 0, 0),
datetime.datetime(2021, 2, 1, 0, 0), datetime.datetime(2021, 2, 8, 0, 0),
datetime.datetime(2021, 2, 15, 0, 0), datetime.datetime(2021, 2, 22, 0, 0),
datetime.datetime(2021, 3, 1, 0, 0)]
my_figure.layout.xaxis['tickmode'] = 'array'
my_figure.layout.xaxis['tickvals'] = weeks_tickvals
my_figure.layout.xaxis['ticktext'] = weeks_ticktext
my_figure.show()
Then, this code will produce the following graph:
Now, I donāt understand why the x axis didnāt start and didnāt get displayed the way I wanted them to be.
I am thankful for any help!
NOTE: I already asked the same question in stackoverflow here