I encountered a problem when adding annotations on my Gantt chart, like the figure below: enter image description here
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: enter image description here
The code I wrote is attached here:
import pandas as pd
import plotly.figure_factory as ff
df = pd.DataFrame( [[Actos 30mg,20190601,20190614,1# QD] , [Ansures ER 500mg,20190605,20190606,1# BID] , [Ansures ER 500mg,20190608,20190612,1# BID]], columns=['drug_id', 'start','finish','usage'])
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.