✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
⚡️ Concerned about the grid? Kyle Baranko teaches how to predicting peak loads using XGBoost. Register for the August webinar!

Gantt plot with annotations on bar

Hello,

I encountered a problem when adding annotations on my Gantt chart, like the figure below:

The problem is that the location of annotations is not where I want them to be. Currently, the annotations is on the top of figure with correct x-axis positions, yet their y-axis positions are not what I want. I hope they can be located (1) on the bar (if the font color is white) or (2) beside the bar (if font color is blue).

The idea is something like this:

The dataframe I used is this:

gantt plot df

The code I wrote is attached here:

import pandas as pd
import plotly.figure_factory as ff

df = pd.read_csv('drug_timeline.csv')
df['start']=pd.to_datetime(df['start'].astype(str), format='%Y%m%d').dt.strftime('%Y-%m-%d')
df['finish']=pd.to_datetime(df['finish'].astype(str), format='%Y%m%d').dt.strftime('%Y-%m-%d')

df['Start'] = df['start']
df['Finish'] = df['finish']
df['Task'] = df['drug_id']
dig = ff.create_gantt(df, group_tasks=True,index_col='drug_id')

fig.show()

fig.update_layout(
    title='Medication Rx',
    yaxis=dict(
        showgrid=False,
        showline=False,
        showticklabels=True,
        domain=[0, 0.9],
    ),
    xaxis=dict(
        zeroline=False,
        showline=False,
        showticklabels=True,
        showgrid=True,
        domain=[0, 0.8],
    ),
    margin=dict(l = 50, r = 50, b = 50, t = 50, pad = 4),
)
dosage= df['usage']
drug = df['drug_id']
date=df['start']

annotations1=[]
for xdn,yd, xd in list(zip(date,dosage,drug)):
    annotations1.append (dict(
                            y=xd, x=xdn,
                            text=str(yd),
                            font=dict(family='consolas', size=12,color='rgb(0,0,205)'),
                            align='left',
                            ))
fig.update_layout(annotations=annotations1,margin=dict(l = 50, r = 50, b = 50, t = 50, pad = 4))
fig.show()

I would like to thank you for your time reading through my problem.

Please let me know any suggestions regarding this annotation issue.