# Label a Gantt Chart

**URL:** <https://community.plotly.com/t/label-a-gantt-chart/7629>\
**Category:** 📊 Plotly Python\
**Created:** [January 9, 2018, 10:41am UTC](https://community.plotly.com/t/label-a-gantt-chart/7629 "2018-01-09T10:41:09Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![benjamin](https://avatars.discourse-cdn.com/v4/letter/b/3d9bf3/32.png) [@benjamin](https://community.plotly.com/u/benjamin)\
**Post date:** [January 9, 2018, 10:41am UTC](https://community.plotly.com/t/label-a-gantt-chart/7629/1 "2018-01-09T10:41:09Z")

</div>

Hello,

How do I add text to be overlaid above each entry. With this:

```auto
		for i in range(len(fig["data"])):
			fig["data"][i].update(text=df[i]["Resource"], mode="text")

```

I get this:

 ![45 AM](https://us1.discourse-cdn.com/flex024/uploads/plot/original/2X/1/13f0c53e030d2d93b89b88695daed9f6df3b3ee1.png)

I would like to have the labels be visible in the center of each region.

Thanks!  
Benjamin

---

<div class="post-metadata">

**Author:** ![benjamin](https://avatars.discourse-cdn.com/v4/letter/b/3d9bf3/32.png) [@benjamin](https://community.plotly.com/u/benjamin)\
**Post date:** [April 1, 2018, 10:32pm UTC](https://community.plotly.com/t/label-a-gantt-chart/7629/2 "2018-04-01T22:32:58Z")

</div>

Still haven’t figured this one out. I’d appreciate any help I can get!

---

<div class="post-metadata">

**Author:** ![benjamin](https://avatars.discourse-cdn.com/v4/letter/b/3d9bf3/32.png) [@benjamin](https://community.plotly.com/u/benjamin)\
**Post date:** [June 19, 2018, 10:10pm UTC](https://community.plotly.com/t/label-a-gantt-chart/7629/3 "2018-06-19T22:10:24Z")

</div>

Bumping… still not sure how to do this

---

<div class="post-metadata">

**Author:** ![isotop](https://avatars.discourse-cdn.com/v4/letter/i/57b2e6/32.png) [@isotop](https://community.plotly.com/u/isotop)\
**Post date:** [June 20, 2018, 7:53am UTC](https://community.plotly.com/t/label-a-gantt-chart/7629/4 "2018-06-20T07:53:03Z")

</div>

I have the same problem, any help would be appreciated

---

<div class="post-metadata">

**Author:** ![benjamin](https://avatars.discourse-cdn.com/v4/letter/b/3d9bf3/32.png) [@benjamin](https://community.plotly.com/u/benjamin)\
**Post date:** [June 21, 2018, 2:56pm UTC](https://community.plotly.com/t/label-a-gantt-chart/7629/5 "2018-06-21T14:56:19Z")

</div>

I gave up on plotly — ended up using the Google Charts timeline instead. It worked out of the box

---

<div class="post-metadata">

**Author:** ![bcd](https://avatars.discourse-cdn.com/v4/letter/b/ec9cab/32.png) [@bcd](https://community.plotly.com/u/bcd)\
**Post date:** [June 22, 2018, 7:08am UTC](https://community.plotly.com/t/label-a-gantt-chart/7629/6 "2018-06-22T07:08:55Z")

</div>

Hey @benjamin and @isotop

Couldn’t you use something like [annotations](https://plot.ly/python/text-and-annotations/#multiple-annotations)?

```
import plotly.plotly as py
import plotly.figure_factory as ff

df = [dict(Task="Job A", Start='2009-01-01', Finish='2009-02-28'),
      dict(Task="Job B", Start='2009-03-05', Finish='2009-04-15'),
      dict(Task="Job C", Start='2009-02-20', Finish='2009-05-30')]

fig = ff.create_gantt(df)

fig['layout']['annotations'] = [dict(x='2009-02-01',y=0,text="This is a label", showarrow=False, font=dict(color='white'))]

py.iplot(fig, world_readable=True)
```

---

<div class="post-metadata">

**Author:** ![aminbe](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@aminbe](https://community.plotly.com/u/aminbe)\
**Post date:** [September 19, 2019, 4:14pm UTC](https://community.plotly.com/t/label-a-gantt-chart/7629/7 "2019-09-19T16:14:21Z")

</div>

Hello,

Yes I was able to use annotations in order to plot labels on gantt chart :  
Here is the used function (you can change it if needed):

```auto
def addAnnot(df, fig):
    for i in df:
        x_pos = (i['Finish'] - i['Start'])/2 + i['Start']
        for j in fig['data']:
            if(j['name'] == i['Label]):
                y_pos = (j['y'][0] + j['y'][1] + j['y'][2] + j['y'][3])/4
        fig['layout']['annotations'] += tuple([dict(x=x_pos,y=y_pos,text=i['Label'],font={'color':'black'})])
    plotly.offline.plot(fig, filename="ganttChart.html")

```

I Hope this would be of some help

Br,

---

<div class="post-metadata">

**Author:** ![ebfa](https://avatars.discourse-cdn.com/v4/letter/e/da6949/32.png) [@ebfa](https://community.plotly.com/u/ebfa)\
**Post date:** [September 28, 2019, 6:44am UTC](https://community.plotly.com/t/label-a-gantt-chart/7629/8 "2019-09-28T06:44:54Z")

</div>

Hello,

Would it be possible for you to explain abit of what you did there?

I’m pretty new to python in general, but I think this is what I need.

BR

---

<div class="post-metadata">

**Author:** ![aminbe](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@aminbe](https://community.plotly.com/u/aminbe)\
**Post date:** [September 28, 2019, 9:01pm UTC](https://community.plotly.com/t/label-a-gantt-chart/7629/9 "2019-09-28T21:01:45Z")

</div>

If you print the gantt chart you will see that it is composed of multiple scatter graphs (boxes filled with different colors), so the idea is to check the different scatters and identify there names and then add them as annotations … the best advice I can give you is to print the gantt chart print(fig) and then try to understand the function I have written

---

<div class="post-metadata">

**Author:** ![ebfa](https://avatars.discourse-cdn.com/v4/letter/e/da6949/32.png) [@ebfa](https://community.plotly.com/u/ebfa)\
**Post date:** [September 30, 2019, 6:08am UTC](https://community.plotly.com/t/label-a-gantt-chart/7629/10 "2019-09-30T06:08:51Z")

</div>

> [@aminbe](#):
>
> is to print the gantt chart print(fig)

Alright I’ll try. Thanks.

---

<div class="post-metadata">

**Author:** ![vvv](https://avatars.discourse-cdn.com/v4/letter/v/41988e/32.png) [@vvv](https://community.plotly.com/u/vvv)\
**Post date:** [March 31, 2020, 1:22pm UTC](https://community.plotly.com/t/label-a-gantt-chart/7629/11 "2020-03-31T13:22:59Z")

</div>

> [@aminbe](#):
>
> def addAnnot(df, fig): for i in df: x\_pos = (i[‘Finish’] - i[‘Start’])/2 + i[‘Start’] for j in fig[‘data’]: if(j[‘name’] == i['Label]): y\_pos = (j[‘y’][0] + j[‘y’][1] + j[‘y’][2] + j[‘y’][3])/4 fig[‘layout’][‘annotations’] += tuple([dict(x=x\_pos,y=y\_pos,text=i[‘Label’],font={‘color’:‘black’})]) plotly.offline.plot(fig, filename=“ganttChart.html”)

----\> 6 x\_pos = (i[‘Finish’] - i[‘Start’]) / 2 + i[‘Start’]  
TypeError: string indices must be integers

I’m surprised that Gantt chart don’t have basic option like label (without this hack) or change the color range and color scale

---

<div class="post-metadata">

**Author:** ![aminbe](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@aminbe](https://community.plotly.com/u/aminbe)\
**Post date:** [April 7, 2021, 3:46pm UTC](https://community.plotly.com/t/label-a-gantt-chart/7629/12 "2021-04-07T15:46:06Z")

</div>

Have you tried to print figure content ? You will then see the data structure inside …
