Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
659 views
in Technique[技术] by (71.8m points)

python - Plotly: How to label bars on a gantt chart?

Is there any way to create in Plotly Gantt chart with Task labels on bars and Resource labels on y-axis?

There is no such example in documentation of plotly.figure_factory.create_gantt. An abstract example of desirable chart is presented below:

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can add annotations structured as a list of dictionaries in fig['layout']['annotations'] based on a setup like the first example from your link.

Plot:

enter image description here

Code:

The structure of the plot is similar to the source above, but I've set it up to work with iplot() in an off-line Jupyter Notebook.

# imports
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
from IPython.core.display import display, HTML
import plotly.figure_factory as ff
import plotly.graph_objs as go

# setup
display(HTML("<style>.container { width:50% !important; } .widget-select > select {background-color: gainsboro;}</style>"))
init_notebook_mode(connected=True)

#%qtconsole --style vim

# dates
StartA = '2009-01-01'
StartB = '2009-03-05'
StartC = '2009-02-20'

FinishA='2009-02-28'
FinishB='2009-04-15'
FinishC='2009-05-30'

LabelDateA='2009-01-25'
LabelDateB='2009-03-20'
LabelDateC='2009-04-01'

# sample data
df = [dict(Task="Task A", Start=StartA, Finish=FinishA),
      dict(Task="Task B", Start=StartB, Finish=FinishB),
      dict(Task="Task C", Start=StartC, Finish=FinishC)]

# figure
fig = ff.create_gantt(df)

# add annotations
annots =  [dict(x=LabelDateA,y=0,text="Task label A", showarrow=False, font=dict(color='white')),
           dict(x=LabelDateB,y=1,text="Task label B", showarrow=False, font=dict(color='White')),
           dict(x=LabelDateC,y=2,text="Task label C", showarrow=False, font=dict(color='White'))]

# plot figure
fig['layout']['annotations'] = annots
iplot(fig)

Possible improvements:

As you can see, I've hard-coded the dates where the labels are positioned. You could just as easily calculate the middle date between the start and end dates. But why not simply adjust the labels using align=center in (x=LabelDateC,y=2,align="center",text="Task label C", showarrow=False, font=dict(color='White'))? That won't work because the label seems to be attached to the dates itself and not the structure or size of the bar.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...