亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用時間戳繪制甘特圖

使用時間戳繪制甘特圖

精慕HU 2023-07-11 17:14:08
我正在嘗試使用plotly 繪制甘特圖。重要的是水平泳道可以有多個在時間上分開的條。我找到了一個使用日歷日期 (YYYY-MM-DD) 的示例,并嘗試使用時間 (HH:MM:SS) 進行轉換。但是當我使用時間戳時,所有內容都會聚集在一起,沒有間隙。import plotly.express as pximport pandas as pddf = pd.DataFrame([    dict(Start='00:01:12', Finish='00:01:59', Resource="Alex"),    dict(Start='00:04:51', Finish='00:05:28', Resource="Alex"),    dict(Start='00:02:12', Finish='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()
查看完整描述

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()

http://img3.sycdn.imooc.com/64ad1d9700015f2912380673.jpg

編輯:不幸的是,甘特圖在不到 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()

http://img3.sycdn.imooc.com/64ad1db10001a4e224191276.jpg

查看完整回答
反對 回復 2023-07-11
  • 1 回答
  • 0 關注
  • 157 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號