简体   繁体   English

plotly 边界得到重叠甘特图

[英]plotly borders getting overlapping gantt chart

please see the below image I am trying to create border to gantt chart, but getting the below issues.请看下图我正在尝试为甘特图创建边框,但遇到以下问题。 Please see my code below, I am getting errors in the graph shown and I am not sure how to proceed请在下面查看我的代码,我在显示的图表中遇到错误,我不确定如何继续

I am trying to create border to gantt chart, but getting the below issues.我正在尝试为甘特图创建边框,但遇到以下问题。 Please see my code below, I am getting errors in the graph shown and I am not sure how to proceed请在下面查看我的代码,我在显示的图表中遇到错误,我不确定如何继续

I am trying to create border to gantt chart, but getting the below issues.我正在尝试为甘特图创建边框,但遇到以下问题。 Please see my code below, I am getting errors in the graph shown and I am not sure how to proceed请在下面查看我的代码,我在显示的图表中遇到错误,我不确定如何继续

import plotly
import plotly.figure_factory as ff

df = [
dict(Task=‘Evening Sleep’, Start=‘2009-01-01’, Finish=‘2009-02-28’, Resource=‘Sleep’, Description = ‘my hover1’ ),
dict(Task=‘Evening Sleep’, Start=‘2009-03-05’, Finish=‘2009-04-15’, Resource=‘Sleep’, Description = ‘my hover1’),
dict(Task=‘Evening Sleep’, Start=‘2009-04-05’, Finish=‘2009-05-15’, Resource=‘Sleep’, Description = ‘my hover1’),
dict(Task=‘Evening Sleep’, Start=‘2009-05-05’, Finish=‘2009-06-15’, Resource=‘Sleep’, Description = ‘my hover1’),
dict(Task=‘Morning Sleep’, Start=‘2009-06-05’, Finish=‘2009-07-15’, Resource=‘Cardio’, Description = ‘my hover1’),
dict(Task=‘Morning Sleep’, Start=‘2009-07-05’, Finish=‘2009-08-15’, Resource=‘Cardio’, Description = ‘my hover1’),
dict(Task=‘Morning Sleep’, Start=‘2009-08-05’, Finish=‘2009-09-15’, Resource=‘Cardio’, Description = ‘my hover1’),
dict(Task=‘Morning Sleep’, Start=‘2009-09-05’, Finish=‘2009-10-15’, Resource=‘Cardio’, Description = ‘my hover1’)

]

colors = dict(Cardio = ‘rgb(46, 137, 205)’,
Food = ‘rgb(114, 44, 121)’,
Sleep = ‘rgb(198, 47, 105)’)

fig = ff.create_gantt(df, colors=colors, index_col=‘Resource’, title=‘Daily Schedule’,show_colorbar=True, showgrid_x=True, showgrid_y=True, group_tasks=True)

#fig = ff.create_gantt(df, group_tasks=True)
fig.update_traces(mode=‘lines’, line_color=‘yellow’, selector=dict(fill=‘toself’))
for trace in fig.data:
trace.x += (trace.x[0],)
trace.y += (trace.y[0],)

fig.show()

fig = ff.create_gantt(df, colors=colors, index_col=‘Resource’, title=‘Daily Schedule’,show_colorbar=True, showgrid_x=True, showgrid_y=True, group_tasks=True)

#fig = ff.create_gantt(df, group_tasks=True)enter code here
import plotly
import plotly.figure_factory as ff

df = [
dict(Task=‘Evening Sleep’, Start=‘2009-01-01’, Finish=‘2009-02-28’, Resource=‘Sleep’, Description = ‘my hover1’ ),
dict(Task=‘Evening Sleep’, Start=‘2009-03-05’, Finish=‘2009-04-15’, Resource=‘Sleep’, Description = ‘my hover1’),
dict(Task=‘Evening Sleep’, Start=‘2009-04-05’, Finish=‘2009-05-15’, Resource=‘Sleep’, Description = ‘my hover1’),
dict(Task=‘Evening Sleep’, Start=‘2009-05-05’, Finish=‘2009-06-15’, Resource=‘Sleep’, Description = ‘my hover1’),
dict(Task=‘Morning Sleep’, Start=‘2009-06-05’, Finish=‘2009-07-15’, Resource=‘Cardio’, Description = ‘my hover1’),
dict(Task=‘Morning Sleep’, Start=‘2009-07-05’, Finish=‘2009-08-15’, Resource=‘Cardio’, Description = ‘my hover1’),
dict(Task=‘Morning Sleep’, Start=‘2009-08-05’, Finish=‘2009-09-15’, Resource=‘Cardio’, Description = ‘my hover1’),
dict(Task=‘Morning Sleep’, Start=‘2009-09-05’, Finish=‘2009-10-15’, Resource=‘Cardio’, Description = ‘my hover1’)

]

colors = dict(Cardio = ‘rgb(46, 137, 205)’,
Food = ‘rgb(114, 44, 121)’,
Sleep = ‘rgb(198, 47, 105)’)

fig = ff.create_gantt(df, colors=colors, index_col=‘Resource’, title=‘Daily Schedule’,show_colorbar=True, showgrid_x=True, showgrid_y=True, group_tasks=True)

#fig = ff.create_gantt(df, group_tasks=True)
fig.update_traces(mode=‘lines’, line_color=‘yellow’, selector=dict(fill=‘toself’))
for trace in fig.data:
trace.x += (trace.x[0],)
trace.y += (trace.y[0],)

fig.show()

fig = ff.create_gantt(df, colors=colors, index_col=‘Resource’, title=‘Daily Schedule’,show_colorbar=True, showgrid_x=True, showgrid_y=True, group_tasks=True)

#fig = ff.create_gantt(df, group_tasks=True)

To add a border to a chart see here , but in summary you need this:要为图表添加边框,请参见此处,但总而言之,您需要:

fig.update_xaxes(showline=True, linewidth=1, linecolor='black', mirror=True)
fig.update_yaxes(showline=True, linewidth=1, linecolor='black', mirror=True)

...but this is not what you really needed. ...但这不是您真正需要的。

To get the full border around each bar you should only need something like:要获得每个栏周围的完整边框,您只需要以下内容:

fig.update_traces(marker_line_color='yellow', marker_line_width=1, opacity=0.6)

...but this method only updates opacity for some reason (but works on other types of bar charts). ...但此方法仅出于某种原因更新不透明度(但适用于其他类型的条形图)。

Using your method you need to add three more points to get the line closed:使用您的方法,您需要再添加三个点来关闭线:

  • (0,None) to distinguish the new line. (0,None) 来区分新行。
  • (0,0) to start up the new line at the beginning (0,0) 在开头启动新行
  • (0, last point) to finish the line (0, last point) 结束行

I've added several more comments in the code below:我在下面的代码中添加了更多注释:

Solution A解决方案 A

import plotly
import plotly.figure_factory as ff

df = [
dict(Task='Evening Sleep', Start='2009-01-01', Finish='2009-02-28', Resource='Sleep', Description = 'my hover1' ),
dict(Task='Evening Sleep', Start='2009-03-05', Finish='2009-04-15', Resource='Sleep', Description = 'my hover1'),
dict(Task='Evening Sleep', Start='2009-04-05', Finish='2009-05-15', Resource='Sleep', Description = 'my hover1'),
dict(Task='Evening Sleep', Start='2009-05-05', Finish='2009-06-15', Resource='Sleep', Description = 'my hover1'),
dict(Task='Morning Sleep', Start='2009-06-05', Finish='2009-07-15', Resource='Cardio', Description = 'my hover1'),
dict(Task='Morning Sleep', Start='2009-07-05', Finish='2009-08-15', Resource='Cardio', Description = 'my hover1'),
dict(Task='Morning Sleep', Start='2009-08-05', Finish='2009-09-15', Resource='Cardio', Description = 'my hover1'),
dict(Task='Morning Sleep', Start='2009-09-05', Finish='2009-10-15', Resource='Cardio', Description = 'my hover1')
]

colors = dict(Cardio = 'rgb(46, 137, 205)', Food = 'rgb(114, 44, 121)',
              Sleep = 'rgb(198, 47, 105)')

fig = ff.create_gantt(df, colors=colors, index_col='Resource', 
                      title='Daily Schedule', show_colorbar=True, 
                      showgrid_x=True, showgrid_y=True, group_tasks=True)

fig.update_traces(mode='lines', line_color='yellow', selector=dict(fill='toself'))
for trace in fig.data:
    # WE NEED TO ADD THREE POINTS HERE:
    #print(trace.x)
    trace.x += (trace.x[0],)
    trace.x += (trace.x[0],)
    trace.x += (trace.x[0],)
    #print(trace.x)
    #print(trace.y)
    trace.y += (trace.y[-5],) #this adds a None to distinguish the new line (from the previous break)
    trace.y += (trace.y[0],)  #this starts us off with the first point
    trace.y += (trace.y[-3],) # this finishes us with the last point (now 3 positions back)
    #print(trace.y)
fig.show()

Also, if you uncomment those print statements you'll see the resulting points before and after.此外,如果您取消注释这些打印语句,您将在前后看到结果点。

Solution A modification解决方案 A 修改

You can also add all three points at once like this (which is a bit cleaner):您也可以像这样一次添加所有三个点(这更简洁):

for trace in fig.data:
    trace.x += (trace.x[0], trace.x[0], trace.x[0])
    trace.y += (trace.y[-5], trace.y[0], trace.y[-1])

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM