plotly.express.timeline 在子图中
plotly.express.timeline in subplots
使用Timelines with 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()
根据 here 的建议,我尝试将其添加到 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()
但它像 graph_objects 一样对待它并且不显示甘特图。
有没有人有任何解决方法或建议?
目前还不清楚为什么会这样,但似乎日期已经de-formatted,所以再次将日期设置为x-axis格式将恢复时间线。
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()
使用Timelines with 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()
根据 here 的建议,我尝试将其添加到 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()
但它像 graph_objects 一样对待它并且不显示甘特图。
有没有人有任何解决方法或建议?
目前还不清楚为什么会这样,但似乎日期已经de-formatted,所以再次将日期设置为x-axis格式将恢复时间线。
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()