子图中的plotly.express.timeline

Amy*_*ski 3 python plotly

使用时间线和plotly.express,我可以获得一个有效的甘特图:

import plotly.express as px
import pandas as pd

df = pd.DataFrame([
    dict(Task="Job A", Start='2009-01-01', Finish='2009-02-28'),
    dict(Task="Job B", Start='2009-03-05', Finish='2009-04-15'),
    dict(Task="Job C", Start='2009-02-20', Finish='2009-05-30')
])

fig = px.timeline(df, x_start="Start", x_end="Finish", y="Task")
fig.update_yaxes(autorange="reversed") # otherwise tasks are listed from the bottom up
fig.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

按照这里的建议,我尝试将其添加到子图中shared_xaxes = True

from plotly.subplots import make_subplots

fig_sub = make_subplots(rows=2, shared_xaxes=True)
fig_sub.append_trace(fig['data'][0], row=1, col=1)
fig_sub.append_trace(fig['data'][0], row=2, col=1)
fig_sub.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

但它将它视为 graph_objects 并且不显示甘特图。

有人有任何解决方法或建议吗?

r-b*_*ers 5

目前尚不清楚为什么会出现这种情况,但日期似乎已被取消格式,因此再次以 x 轴格式设置日期将恢复时间线。

from plotly.subplots import make_subplots

fig_sub = make_subplots(rows=2, shared_xaxes=True)
fig_sub.append_trace(fig['data'][0], row=1, col=1)
fig_sub.append_trace(fig['data'][0], row=2, col=1)

fig_sub.update_xaxes(type='date')
fig_sub.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述