@barg (barg barg)

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

python plotly timelineで2軸作成する方法を教えて下さい

解決したいこと

現在pythonでplotly timeline(ガントチャート)を作ってHTMLに変換してWEBで表示しています
項目1つに対して2軸並べて表示させたいのですが、plotly timelineでは不可能でしょうか?
どなたかご存じでしたら教えて下さい。

0 likes

2Answer

こんな感じですか?

newplot.png

実装は以下のようになっています。

# coding: utf-8
import pandas as pd
import plotly.express as px

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

fig = px.timeline(df, x_start="Start", x_end="Finish", y="Task", color="Resource", title="Gantt chart")
fig.update_yaxes(autorange="reversed")
# fig.show()

f2 = fig.full_figure_for_development(warn=False)
f2.layout.barmode = 'group'
f2.show()

※注 警告が出たら、以下をターミナルで実行してください。

$ pip install -U kaleido

参考

0Like

Comments

  1. @barg

    Questioner

    ありがとうございます。2軸作成する事が出来ました
    しかし、2軸にするとWEBに表示した時、サイズが小さくなっていまいました
    その対策として
    fig.update_layout(template="plotly_dark",dragmode='pan',newshape_line_color='red',width=1100)
    widthを設定したのですが、レスポンシブ対応しなくなりました
    他の設定方法ご存じでしたら教えてください

コメントではmarkdownが書けないのでこちらで回答します。

回答後、私もいろいろ調べてみたら、full_figure_for_developmentでわざわざ新しく図を生成する必要がなかったみたいです。

以下、当該のソースコードです。
これならWebページの大きさに合わせてグラフも変化してくれます。

# coding: utf-8
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go

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

fig = px.timeline(df, x_start="Start", x_end="Finish", y="Task", color="Resource", title="Gantt chart")

fig.update_layout(
	template="plotly_dark",
	dragmode='pan',
	newshape_line_color='red',
	barmode="group"
)

fig.update_yaxes(autorange="reversed")

fig.show()
0Like

Comments

  1. @barg

    Questioner

    ありがとうございました。レスポンシブ直りました

Your answer might help someone💌