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