Black Lives Matter. Please consider donating to Black Girls Code today.

Trying to add multiple annotations on a gantt chart

I’m using this code for that task, the tsv file i’m reading in the code has a similar format to the dicts i pasted on the end of the code… The problem is, when i run the code, the last annotation the code makes is the only one that shows in the exported chart, also, how do i make the annotations go to the center of the block?

import plotly.plotly as py
import plotly.figure_factory as ff
import plotly.graph_objs as go
import plotly
import csv

def readtsv():
      lista = []
      with open('output.tsv') as csvfile:
            reader = csv.reader(csvfile, delimiter="\t")
            fields = ["Task", "Start", "Finish", "Resource"]
            for row in reader:
                  lista.append({key: value for key, value in zip(fields, row)})
            fig = ff.create_gantt(lista, index_col='Resource', show_colorbar=True, group_tasks=True)

      #fig.layout.xaxis.tickformat = '%Y'
      anotar(lista, fig)
      plotly.offline.plot(fig, filename='dildo.html')

    
def anotar(basura, fig):
      for i in basura:
            c=0
            print (i.get("Start"))
            fig['layout']['annotations'] = [dict(x=i.get("Start"),y=c,text="labeled", showarrow=True, font=dict(color='black'))]
            plotly.offline.plot(fig, filename='dildo.html')

            c=c+1


'''def anotar(basura):
      trash = []
      for i in basura:                          
            c=0
            print (i.get("Start")) 
            trash.append= [dict(x=i.get("Start"),y=c,text="labeled", showarrow=True, font=dict(color='black'))]
            c=c+1
      layout = go.Layout(showlegend=False,annotations=[trash])
      return layout
'''

if __name__ == "__main__":
      df = [dict(Task='1', Start='0635', Finish='0785', Resource='Maquina 4'),
      dict(Task='1', Start='0087', Finish='0334', Resource='Maquina 1'),
      dict(Task='1', Start='0334', Finish='0457', Resource='Maquina 7')]
      
      readtsv()

Hi @anonimeishon,

It looks like this line is overwriting all annotations during each iteration through the for loop

fig['layout']['annotations'] = [dict(x=i.get("Start"),y=c,text="labeled", showarrow=True, font=dict(color='black'))]

Try something like…

fig['layout']['annotations'] += tuple([dict(x=i.get("Start"),y=c,text="labeled", showarrow=True, font=dict(color='black'))])

for centering the annotations, you could try adjusting the annotation.xanchor property (See https://plot.ly/python/reference/#layout-annotations-items-annotation-xanchor)

Sets the text box’s horizontal position anchor This anchor binds the x position to the “left”, “center” or “right” of the annotation. For example, if x is set to 1, xref to “paper” and xanchor to “right” then the right-most portion of the annotation lines up with the right-most edge of the plotting area. If “auto”, the anchor is equivalent to “center” for data-referenced annotations or if there is an arrow, whereas for paper-referenced with no arrow, the anchor picked corresponds to the closest side.

-Jon