Movatterモバイル変換


[0]ホーム

URL:


Quick Reference
On This Page
Let Your Data Vibe: From Dataset to Data App with Agentic Analytics

Gantt Charts in Python

How to make Gantt Charts in Python with Plotly. Gantt Charts use horizontal bars to represent the start and end times of tasks.


Plotly Studio: Transform any dataset into an interactive data application in minutes with AI.Sign up for early access now.

AGantt chart is a type of bar chart that illustrates a project schedule. The chart lists the tasks to be performed on the vertical axis, and time intervals on the horizontal axis. The width of the horizontal bars in the graph shows the duration of each activity.

Gantt Charts and Timelines with plotly.express

Plotly Express is the easy-to-use, high-level interface to Plotly, whichoperates on a variety of types of data and produceseasy-to-style figures. Withpx.timeline (introduced in version 4.9) each data point is represented as a horizontal bar with a start and end point specified as dates.

Thepx.timeline function by default sets the X-axis to be oftype=date, so it can be configured like anytime-series chart.

Plotly Express also supports ageneral-purposepx.bar function for bar charts.

In [1]:
importplotly.expressaspximportpandasaspddf=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 upfig.show()

px.timeline supportsdiscrete color as above, orcontinuous color as follows.

In [2]:
importplotly.expressaspximportpandasaspddf=pd.DataFrame([dict(Task="Job A",Start='2009-01-01',Finish='2009-02-28',Resource="Alex"),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="Max")])fig=px.timeline(df,x_start="Start",x_end="Finish",y="Task",color="Resource")fig.update_yaxes(autorange="reversed")fig.show()
In [3]:
importplotly.expressaspximportpandasaspddf=pd.DataFrame([dict(Task="Job A",Start='2009-01-01',Finish='2009-02-28',Completion_pct=50),dict(Task="Job B",Start='2009-03-05',Finish='2009-04-15',Completion_pct=25),dict(Task="Job C",Start='2009-02-20',Finish='2009-05-30',Completion_pct=75)])fig=px.timeline(df,x_start="Start",x_end="Finish",y="Task",color="Completion_pct")fig.update_yaxes(autorange="reversed")fig.show()

It is also possible to have multiple bars on the same horizontal line, say by resource:

Note: When settingcolor to the same value asy,autorange should not be set toreverse, so as to list the value of the Y axis in the same order as the legend entries.

In [4]:
importplotly.expressaspximportpandasaspddf=pd.DataFrame([dict(Task="Job A",Start='2009-01-01',Finish='2009-02-28',Resource="Alex"),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="Max")])fig=px.timeline(df,x_start="Start",x_end="Finish",y="Resource",color="Resource")fig.show()

Deprecated Figure Factory

Prior to the introduction ofplotly.express.timeline() in version 4.9, the recommended way to make Gantt charts was to use the now-deprecatedcreate_gantt()figure factory, as follows:

In [5]:
importplotly.figure_factoryasffdf=[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=ff.create_gantt(df)fig.show()

Group Tasks Together

The following example shows how to use the now-deprecatedcreate_gantt()figure factory to color tasks by a numeric variable.

In [6]:
importplotly.figure_factoryasffdf=[dict(Task="Job-1",Start='2017-01-01',Finish='2017-02-02',Resource='Complete'),dict(Task="Job-1",Start='2017-02-15',Finish='2017-03-15',Resource='Incomplete'),dict(Task="Job-2",Start='2017-01-17',Finish='2017-02-17',Resource='Not Started'),dict(Task="Job-2",Start='2017-01-17',Finish='2017-02-17',Resource='Complete'),dict(Task="Job-3",Start='2017-03-10',Finish='2017-03-20',Resource='Not Started'),dict(Task="Job-3",Start='2017-04-01',Finish='2017-04-20',Resource='Not Started'),dict(Task="Job-3",Start='2017-05-18',Finish='2017-06-18',Resource='Not Started'),dict(Task="Job-4",Start='2017-01-14',Finish='2017-03-14',Resource='Complete')]colors={'Not Started':'rgb(220, 0, 0)','Incomplete':(1,0.9,0.16),'Complete':'rgb(0, 255, 100)'}fig=ff.create_gantt(df,colors=colors,index_col='Resource',show_colorbar=True,group_tasks=True)fig.show()

Color by Numeric Variable

The following example shows how to use the now-deprecatedcreate_gantt()figure factory to color tasks by a numeric variable.

In [7]:
importplotly.figure_factoryasffdf=[dict(Task="Job A",Start='2009-01-01',Finish='2009-02-28',Complete=10),dict(Task="Job B",Start='2008-12-05',Finish='2009-04-15',Complete=60),dict(Task="Job C",Start='2009-02-20',Finish='2009-05-30',Complete=95)]fig=ff.create_gantt(df,colors='Viridis',index_col='Complete',show_colorbar=True)fig.show()

Reference

For more info onff.create_gantt(), see thefull function reference

What About Dash?

Dash is an open-source framework for building analytical applications, with no Javascript required, and it is tightly integrated with the Plotly graphing library.

Learn about how to install Dash athttps://dash.plot.ly/installation.

Everywhere in this page that you seefig.show(), you can display the same figure in a Dash application by passing it to thefigure argument of theGraph component from the built-indash_core_components package like this:

importplotly.graph_objectsasgo# or plotly.express as pxfig=go.Figure()# or any Plotly Express function e.g. px.bar(...)# fig.add_trace( ... )# fig.update_layout( ... )fromdashimportDash,dcc,htmlapp=Dash()app.layout=html.Div([dcc.Graph(figure=fig)])app.run(debug=True,use_reloader=False)# Turn off reloader if inside Jupyter

[8]ページ先頭

©2009-2025 Movatter.jp