Plotly:如何在甘特图上标记条形图?
Plotly: How to label bars on a gantt chart?
有什么方法可以在 Plotly 甘特图中创建条形图上的任务标签和 y 轴上的资源标签?
在plotly.figure_factory.create_gantt
的documentation中没有这样的例子。理想图表的抽象示例如下:
您可以根据 your link.
中第一个示例的设置,在 fig['layout']['annotations']
中添加结构为字典列表的注释
剧情:
代码:
情节的结构与上面的来源相似,但我已将其设置为在 off-line Jupyter Notebook 中使用 iplot()
。
# 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)
可能的改进:
如您所见,我已经 hard-coded 放置标签的日期。您可以在开始日期和结束日期之间轻松 calculate the middle date。但为什么不使用 (x=LabelDateC,y=2,align="center",text="Task label C", showarrow=False, font=dict(color='White'))
中的 align=center
简单地调整标签呢?这行不通,因为标签似乎附加到日期本身,而不是条形的结构或大小。
有什么方法可以在 Plotly 甘特图中创建条形图上的任务标签和 y 轴上的资源标签?
在plotly.figure_factory.create_gantt
的documentation中没有这样的例子。理想图表的抽象示例如下:
您可以根据 your link.
中第一个示例的设置,在fig['layout']['annotations']
中添加结构为字典列表的注释
剧情:
代码:
情节的结构与上面的来源相似,但我已将其设置为在 off-line Jupyter Notebook 中使用 iplot()
。
# 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)
可能的改进:
如您所见,我已经 hard-coded 放置标签的日期。您可以在开始日期和结束日期之间轻松 calculate the middle date。但为什么不使用 (x=LabelDateC,y=2,align="center",text="Task label C", showarrow=False, font=dict(color='White'))
中的 align=center
简单地调整标签呢?这行不通,因为标签似乎附加到日期本身,而不是条形的结构或大小。