Plotly:如何在甘特图上标记条形?

Plotly: How to label bars on a gantt chart?(Plotly:如何在甘特图上标记条形?)
本文介绍了Plotly:如何在甘特图上标记条形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以在 Plotly Gantt 图中创建条形图上的任务标签和 y 轴上的资源标签?

plotly.figure_factory.create_gantt的

解决方案

您可以根据设置在 fig['layout']['annotations'] 中添加结构为字典列表的注释就像

代码:

情节的结构与上面的源代码相似,但我已将其设置为在离线 Jupyter Notebook 中与 iplot() 一起使用.

# 导入从 plotly.offline 导入 download_plotlyjs, init_notebook_mode, plot, iplot从 IPython.core.display 导入显示,HTML将 plotly.figure_factory 导入为 ff导入 plotly.graph_objs# 设置display(HTML("<style>.container { width:50% !important; } .widget-select > select {background-color: gainboro;}</style>"))init_notebook_mode(连接=真)#%qtconsole --style vim# 日期开始A = '2009-01-01'StartB = '2009-03-05'StartC = '2009-02-20'FinishA='2009-02-28'FinishB='2009-04-15'FinishC='2009-05-30'标签日期A='2009-01-25'标签日期B='2009-03-20'LabelDateC='2009-04-01'# 样本数据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)]# 数字图 = ff.create_gantt(df)# 添加注释annots = [dict(x=LabelDateA,y=0,text="任务标签 A", showarrow=False, font=dict(color='white')),dict(x=LabelDateB,y=1,text="任务标签 B", showarrow=False, font=dict(color='White')),dict(x=LabelDateC,y=2,text="任务标签 C", showarrow=False, font=dict(color='White'))]# 绘制图形fig['layout']['annotations'] = annotsiplot(无花果)

可能的改进:

如您所见,我已经硬编码了标签所在的日期.您可以轻松地计算中间日期开始和结束日期.但是为什么不简单地使用 (x=LabelDateC,y=2,align="center",text="Task label C", showarrow=False, font 中的 align=center 调整标签=dict(color='White'))?这不起作用,因为标签似乎附加到日期本身,而不是条的结构或大小.

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:

解决方案

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:

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.

这篇关于Plotly:如何在甘特图上标记条形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Leetcode 234: Palindrome LinkedList(Leetcode 234:回文链接列表)
How do I read an Excel file directly from Dropbox#39;s API using pandas.read_excel()?(如何使用PANDAS.READ_EXCEL()直接从Dropbox的API读取Excel文件?)
subprocess.Popen tries to write to nonexistent pipe(子进程。打开尝试写入不存在的管道)
I want to realize Popen-code from Windows to Linux:(我想实现从Windows到Linux的POpen-code:)
Reading stdout from a subprocess in real time(实时读取子进程中的标准输出)
How to call type safely on a random file in Python?(如何在Python中安全地调用随机文件上的类型?)