简体   繁体   中英

How to Customize datetime ticks in Plotly (Python)

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 my chart:

在此处输入图像描述

Nice, but for the x axis ticks. I'd like to customize them to fit my needs:

  1. 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

  2. Instead of “04 Jan”, the tick labels should be something like this: “KW: 01 ('21)”. Here, KW is supposed to mean calendar week.

  3. 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 also asked the same question in the Dash Community , but didn't get no help so far.

I am thankful for any comment!

1) To set the start date you can adjust the axis range with something like

my_figure.layout['xaxis'].update(range=[min, max])

where you have to figure out how to find min and max of your labels.

2) As documented here the easiest way to achieve what you are looking for is to set the xaxis_tickformat in the layout. Try adding the line

my_figure.layout['xaxis_tickformat'] = '%d %B whatever <br>%Y'

just before the last line and play with the format string as explained and documented here

3) Probably all this customization will lead you to write a simple function that returns lists of strings to use as ticks. If you want to stick with the datetime objects you could check this post (with no solution) as an entry point for your search.

Good coding!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM