1 回答

TA貢獻1801條經驗 獲得超16個贊
Plotly 甘特圖僅適用于日期,但可能的解決方法是將日期添加1970-01-01
到所有時間的開頭,然后顯示時間而不在圖中顯示日期。
import plotly.express as px
import pandas as pd
df = pd.DataFrame([
? ? dict(Start='1970-01-01 00:01:12', Finish='1970-01-01 00:01:59', Resource="Alex"),
? ? dict(Start='1970-01-01 00:04:51', Finish='1970-01-01 00:05:28', Resource="Alex"),
? ? dict(Start='1970-01-01 00:02:12', Finish='1970-01-01 00:04:34', Resource="Max")
])
fig = px.timeline(df, x_start="Start", x_end="Finish", y="Resource", color="Resource"
? ? ? ? ? ? ? ? ?)
fig.update_layout(xaxis=dict(
? ? ? ? ? ? ? ? ? ? ? title='Timestamp',?
? ? ? ? ? ? ? ? ? ? ? tickformat = '%H:%M:%S',
? ? ? ? ? ? ? ? ? ))
fig.show()
編輯:不幸的是,甘特圖在不到 10 秒的時間間隔內中斷,我不明白為什么。然而,我非常確定,在幕后,甘特圖只不過是在圖表上繪制的矩形,因此我們可以繪制這樣一個小于 10 秒的間隔來實現類似的效果(除了沒有懸停手動繪制的形狀)
import plotly.express as px
import pandas as pd
df = pd.DataFrame([
? ? # dict(Start='1970-01-01 00:01:12', Finish='1970-01-01 00:01:19', Resource="Alex"),
? ? dict(Start='1970-01-01 00:04:51', Finish='1970-01-01 00:05:28', Resource="Alex"),
? ? dict(Start='1970-01-01 00:02:12', Finish='1970-01-01 00:04:34', Resource="Max")
])
fig = px.timeline(df, x_start="Start", x_end="Finish", y="Resource", color="Resource"
? ? ? ? ? ? ? ? ?)
# you can manually set the range as well
fig.update_layout(xaxis=dict(
? ? ? ? ? ? ? ? ? ? ? title='Timestamp',?
? ? ? ? ? ? ? ? ? ? ? tickformat = '%H:%M:%S',
? ? ? ? ? ? ? ? ? ? ? range = ['1970-01-01 00:01:00','1970-01-01 00:06:00']
? ? ? ? ? ? ? ? ? ))
# add a filled rectangle
fig.add_shape(
? ? ? ? ? ? type="rect",
? ? ? ? ? ? x0='1970-01-01 00:01:12',
? ? ? ? ? ? y0=0.6,
? ? ? ? ? ? x1='1970-01-01 00:01:19',
? ? ? ? ? ? y1=1.4,
? ? ? ? ? ? line=dict(color="rgb(98,115,241)"),
? ? ? ? ? ? fillcolor="rgb(98,115,241)",
? ? ? ? )
fig.show()
添加回答
舉報