【发布时间】:2021-04-29 14:34:09
【问题描述】:
我有两个不同的数据框,其中包含不同的列:开始和结束日期、持续时间...
我想使用 plotly express 将数据绘制到时间线(甘特图)中。我知道如何使用子图,但是我希望它们在同一个图形上而不是图上。
这是我的尝试:
'''
import plotly.graph_objs as go
import plotly.express as px
fig = px.timeline(chosen_data3, x_start="Start Date", x_end="End Date", y="new" )
fig2 = px.timeline(chosen_data4, x_start="Start Date", x_end="End Date", y="new", ax = fig )
fig2.show()
#Using graph_objs :
fig = go.Figure()
for (start, end, value, value2) in zip(chosen_data3["Start Date"], chosen_data3["End Date"],
chosen_data3["Duration [Hours]"],chosen_data3["Hs [m]"] ):
name = f"Towing : {start} to {end}"
fig.add_trace(go.Scatter(x=[start, end], y=[value, value],mode='lines', name = name , marker =
dict(color = 'rgb(0,0,0)'), hoverinfo = 'all'))
for (start, end, value, value2) in zip(chosen_data4["Start Date"], chosen_data4["End Date"], chosen_data4["Duration [Hours]"], chosen_data4["Hs [m]"]):
name = f"Hook-Up :{start} to {end}"
fig.add_trace(go.Scatter(x=[start, end], y=[value, value],mode='lines', name = name, marker = dict(color = 'rgb(65,105,225)'), hoverinfo = 'all'))
fig.show()
'''
graph_objs 的解决方案是正确的,但是使用 express 的解决方案不正确
编辑: 我的两个数据框示例:
使用 Graph objs 的解决方案:
我尝试的解决方案是连接两个数据框并创建一个新列来区分每个数据,当您将鼠标悬停在每个条上时,我会使用 plotly express 时间轴得到下图,您会看到所有需要的信息:
【问题讨论】:
-
我是模棱两可还是不清楚?在那种情况下我可以重新制定...
-
您能否包含您的 DataFrame 示例,并包含使用 graph_objects 的解决方案的图像?这将帮助人们回答您的问题,因为您现在编写它的方式是不可复制的。
-
你去吧,我希望它更清楚
-
是的,清楚多了!希望有人能帮助回答您的问题,稍后我有时间再看一下
标签: python pandas plotly gantt-chart