{ "cells": [ { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "#### New to Plotly?\n", "Plotly's Python library is free and open source! [Get started](https://plotly.com/python/getting-started/) by downloading the client and [reading the primer](https://plotly.com/python/getting-started/).\n", "
You can set up Plotly to work in [online](https://plotly.com/python/getting-started/#initialization-for-online-plotting) or [offline](https://plotly.com/python/getting-started/#initialization-for-offline-plotting) mode, or in [jupyter notebooks](https://plotly.com/python/getting-started/#start-plotting-online).\n", "
We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf) (new!) to help you get started!" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "#### Version Check\n", "Note: Gantt Charts are available in version 1.12.2+
\n", "Run `pip install plotly --upgrade` to update your Plotly version" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [ "'2.0.5'" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly\n", "plotly.__version__" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "#### Simple Gantt Chart" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly.plotly as py\n", "import plotly.figure_factory as ff\n", "\n", "df = [dict(Task=\"Job A\", Start='2009-01-01', Finish='2009-02-28'),\n", " dict(Task=\"Job B\", Start='2009-03-05', Finish='2009-04-15'),\n", " dict(Task=\"Job C\", Start='2009-02-20', Finish='2009-05-30')]\n", "\n", "fig = ff.create_gantt(df)\n", "py.iplot(fig, filename='gantt-simple-gantt-chart', world_readable=True)" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "#### Index by Numeric Variable" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly.plotly as py\n", "import plotly.figure_factory as ff\n", "\n", "df = [dict(Task=\"Job A\", Start='2009-01-01', Finish='2009-02-28', Complete=10),\n", " dict(Task=\"Job B\", Start='2008-12-05', Finish='2009-04-15', Complete=60),\n", " dict(Task=\"Job C\", Start='2009-02-20', Finish='2009-05-30', Complete=95)]\n", "\n", "fig = ff.create_gantt(df, colors='Viridis', index_col='Complete', show_colorbar=True)\n", "py.iplot(fig, filename='gantt-numeric-variable', world_readable=True)" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "#### Index by String Variable" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly.plotly as py\n", "import plotly.figure_factory as ff\n", "\n", "df = [dict(Task=\"Job A\", Start='2009-01-01', Finish='2009-02-01', Resource='Apple'),\n", " dict(Task=\"Job B\", Start='2009-03-05', Finish='2009-04-15', Resource='Grape'),\n", " dict(Task=\"Job C\", Start='2009-04-20', Finish='2009-09-30', Resource='Banana')]\n", "\n", "colors = ['#7a0504', (0.2, 0.7, 0.3), 'rgb(210, 60, 180)']\n", "\n", "fig = ff.create_gantt(df, colors=colors, index_col='Resource', reverse_colors=True, show_colorbar=True)\n", "py.iplot(fig, filename='gantt-string-variable', world_readable=True)" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "#### Use a Dictionary for Colors" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly.plotly as py\n", "import plotly.figure_factory as ff\n", "\n", "df = [dict(Task=\"Job A\", Start='2016-01-01', Finish='2016-01-02', Resource='Apple'),\n", " dict(Task=\"Job B\", Start='2016-01-02', Finish='2016-01-04', Resource='Grape'),\n", " dict(Task=\"Job C\", Start='2016-01-02', Finish='2016-01-03', Resource='Banana')]\n", "\n", "colors = dict(Apple = 'rgb(220, 0, 0)',\n", " Grape = 'rgb(170, 14, 200)',\n", " Banana = (1, 0.9, 0.16))\n", "\n", "fig = ff.create_gantt(df, colors=colors, index_col='Resource', show_colorbar=True)\n", "py.iplot(fig, filename='gantt-dictioanry-colors', world_readable=True)" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "#### Use a Pandas Dataframe" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly.plotly as py\n", "import plotly.figure_factory as ff\n", "\n", "import pandas as pd\n", "\n", "df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/gantt_example.csv')\n", "\n", "fig = ff.create_gantt(df, colors=['#333F44', '#93e4c1'], index_col='Complete', show_colorbar=True,\n", " bar_width=0.2, showgrid_x=True, showgrid_y=True)\n", "py.iplot(fig, filename='gantt-use-a-pandas-dataframe', world_readable=True)" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "#### Using Hours and Minutes in Times" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly.plotly as py\n", "import plotly.figure_factory as ff\n", "\n", "df = [\n", " dict(Task='Morning Sleep', Start='2016-01-01', Finish='2016-01-01 6:00:00', Resource='Sleep'),\n", " dict(Task='Breakfast', Start='2016-01-01 7:00:00', Finish='2016-01-01 7:30:00', Resource='Food'),\n", " dict(Task='Work', Start='2016-01-01 9:00:00', Finish='2016-01-01 11:25:00', Resource='Brain'),\n", " dict(Task='Break', Start='2016-01-01 11:30:00', Finish='2016-01-01 12:00:00', Resource='Rest'),\n", " dict(Task='Lunch', Start='2016-01-01 12:00:00', Finish='2016-01-01 13:00:00', Resource='Food'),\n", " dict(Task='Work', Start='2016-01-01 13:00:00', Finish='2016-01-01 17:00:00', Resource='Brain'),\n", " dict(Task='Exercise', Start='2016-01-01 17:30:00', Finish='2016-01-01 18:30:00', Resource='Cardio'), \n", " dict(Task='Post Workout Rest', Start='2016-01-01 18:30:00', Finish='2016-01-01 19:00:00', Resource='Rest'),\n", " dict(Task='Dinner', Start='2016-01-01 19:00:00', Finish='2016-01-01 20:00:00', Resource='Food'),\n", " dict(Task='Evening Sleep', Start='2016-01-01 21:00:00', Finish='2016-01-01 23:59:00', Resource='Sleep')\n", "]\n", "\n", "colors = dict(Cardio = 'rgb(46, 137, 205)',\n", " Food = 'rgb(114, 44, 121)',\n", " Sleep = 'rgb(198, 47, 105)',\n", " Brain = 'rgb(58, 149, 136)',\n", " Rest = 'rgb(107, 127, 135)')\n", "\n", "fig = ff.create_gantt(df, colors=colors, index_col='Resource', title='Daily Schedule',\n", " show_colorbar=True, bar_width=0.8, showgrid_x=True, showgrid_y=True)\n", "py.iplot(fig, filename='gantt-hours-minutes', world_readable=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Group Tasks Together" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly.plotly as py\n", "import plotly.figure_factory as ff\n", "\n", "df = [dict(Task=\"Job-1\", Start='2017-01-01', Finish='2017-02-02', Resource='Complete'),\n", " dict(Task=\"Job-1\", Start='2017-02-15', Finish='2017-03-15', Resource='Incomplete'),\n", " dict(Task=\"Job-2\", Start='2017-01-17', Finish='2017-02-17', Resource='Not Started'),\n", " dict(Task=\"Job-2\", Start='2017-01-17', Finish='2017-02-17', Resource='Complete'),\n", " dict(Task=\"Job-3\", Start='2017-03-10', Finish='2017-03-20', Resource='Not Started'),\n", " dict(Task=\"Job-3\", Start='2017-04-01', Finish='2017-04-20', Resource='Not Started'),\n", " dict(Task=\"Job-3\", Start='2017-05-18', Finish='2017-06-18', Resource='Not Started'),\n", " dict(Task=\"Job-4\", Start='2017-01-14', Finish='2017-03-14', Resource='Complete')]\n", "\n", "colors = {'Not Started': 'rgb(220, 0, 0)',\n", " 'Incomplete': (1, 0.9, 0.16),\n", " 'Complete': 'rgb(0, 255, 100)'}\n", "\n", "fig = ff.create_gantt(df, colors=colors, index_col='Resource', show_colorbar=True, group_tasks=True)\n", "py.iplot(fig, filename='gantt-group-tasks-together', world_readable=True)" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "#### Reference\n", "For info on Plotly Range Slider and Selector, see: https://plotly.com/python/reference/#layout-xaxis-rangeselector." ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on function create_gantt in module plotly.figure_factory._gantt:\n", "\n", "create_gantt(df, colors=None, index_col=None, show_colorbar=False, reverse_colors=False, title='Gantt Chart', bar_width=0.2, showgrid_x=False, showgrid_y=False, height=600, width=900, tasks=None, task_names=None, data=None, group_tasks=False)\n", " Returns figure for a gantt chart\n", " \n", " :param (array|list) df: input data for gantt chart. Must be either a\n", " a dataframe or a list. If dataframe, the columns must include\n", " 'Task', 'Start' and 'Finish'. Other columns can be included and\n", " used for indexing. If a list, its elements must be dictionaries\n", " with the same required column headers: 'Task', 'Start' and\n", " 'Finish'.\n", " :param (str|list|dict|tuple) colors: either a plotly scale name, an\n", " rgb or hex color, a color tuple or a list of colors. An rgb color\n", " is of the form 'rgb(x, y, z)' where x, y, z belong to the interval\n", " [0, 255] and a color tuple is a tuple of the form (a, b, c) where\n", " a, b and c belong to [0, 1]. If colors is a list, it must\n", " contain the valid color types aforementioned as its members.\n", " If a dictionary, all values of the indexing column must be keys in\n", " colors.\n", " :param (str|float) index_col: the column header (if df is a data\n", " frame) that will function as the indexing column. If df is a list,\n", " index_col must be one of the keys in all the items of df.\n", " :param (bool) show_colorbar: determines if colorbar will be visible.\n", " Only applies if values in the index column are numeric.\n", " :param (bool) reverse_colors: reverses the order of selected colors\n", " :param (str) title: the title of the chart\n", " :param (float) bar_width: the width of the horizontal bars in the plot\n", " :param (bool) showgrid_x: show/hide the x-axis grid\n", " :param (bool) showgrid_y: show/hide the y-axis grid\n", " :param (float) height: the height of the chart\n", " :param (float) width: the width of the chart\n", " \n", " Example 1: Simple Gantt Chart\n", " ```\n", " import plotly.plotly as py\n", " from plotly.figure_factory import create_gantt\n", " \n", " # Make data for chart\n", " df = [dict(Task=\"Job A\", Start='2009-01-01', Finish='2009-02-30'),\n", " dict(Task=\"Job B\", Start='2009-03-05', Finish='2009-04-15'),\n", " dict(Task=\"Job C\", Start='2009-02-20', Finish='2009-05-30')]\n", " \n", " # Create a figure\n", " fig = create_gantt(df)\n", " \n", " # Plot the data\n", " py.iplot(fig, filename='Simple Gantt Chart', world_readable=True)\n", " ```\n", " \n", " Example 2: Index by Column with Numerical Entries\n", " ```\n", " import plotly.plotly as py\n", " from plotly.figure_factory import create_gantt\n", " \n", " # Make data for chart\n", " df = [dict(Task=\"Job A\", Start='2009-01-01',\n", " Finish='2009-02-30', Complete=10),\n", " dict(Task=\"Job B\", Start='2009-03-05',\n", " Finish='2009-04-15', Complete=60),\n", " dict(Task=\"Job C\", Start='2009-02-20',\n", " Finish='2009-05-30', Complete=95)]\n", " \n", " # Create a figure with Plotly colorscale\n", " fig = create_gantt(df, colors='Blues', index_col='Complete',\n", " show_colorbar=True, bar_width=0.5,\n", " showgrid_x=True, showgrid_y=True)\n", " \n", " # Plot the data\n", " py.iplot(fig, filename='Numerical Entries', world_readable=True)\n", " ```\n", " \n", " Example 3: Index by Column with String Entries\n", " ```\n", " import plotly.plotly as py\n", " from plotly.figure_factory import create_gantt\n", " \n", " # Make data for chart\n", " df = [dict(Task=\"Job A\", Start='2009-01-01',\n", " Finish='2009-02-30', Resource='Apple'),\n", " dict(Task=\"Job B\", Start='2009-03-05',\n", " Finish='2009-04-15', Resource='Grape'),\n", " dict(Task=\"Job C\", Start='2009-02-20',\n", " Finish='2009-05-30', Resource='Banana')]\n", " \n", " # Create a figure with Plotly colorscale\n", " fig = create_gantt(df, colors=['rgb(200, 50, 25)', (1, 0, 1), '#6c4774'],\n", " index_col='Resource', reverse_colors=True,\n", " show_colorbar=True)\n", " \n", " # Plot the data\n", " py.iplot(fig, filename='String Entries', world_readable=True)\n", " ```\n", " \n", " Example 4: Use a dictionary for colors\n", " ```\n", " import plotly.plotly as py\n", " from plotly.figure_factory import create_gantt\n", " \n", " # Make data for chart\n", " df = [dict(Task=\"Job A\", Start='2009-01-01',\n", " Finish='2009-02-30', Resource='Apple'),\n", " dict(Task=\"Job B\", Start='2009-03-05',\n", " Finish='2009-04-15', Resource='Grape'),\n", " dict(Task=\"Job C\", Start='2009-02-20',\n", " Finish='2009-05-30', Resource='Banana')]\n", " \n", " # Make a dictionary of colors\n", " colors = {'Apple': 'rgb(255, 0, 0)',\n", " 'Grape': 'rgb(170, 14, 200)',\n", " 'Banana': (1, 1, 0.2)}\n", " \n", " # Create a figure with Plotly colorscale\n", " fig = create_gantt(df, colors=colors, index_col='Resource',\n", " show_colorbar=True)\n", " \n", " # Plot the data\n", " py.iplot(fig, filename='dictioanry colors', world_readable=True)\n", " ```\n", " \n", " Example 5: Use a pandas dataframe\n", " ```\n", " import plotly.plotly as py\n", " from plotly.figure_factory import create_gantt\n", " \n", " import pandas as pd\n", " \n", " # Make data as a dataframe\n", " df = pd.DataFrame([['Run', '2010-01-01', '2011-02-02', 10],\n", " ['Fast', '2011-01-01', '2012-06-05', 55],\n", " ['Eat', '2012-01-05', '2013-07-05', 94]],\n", " columns=['Task', 'Start', 'Finish', 'Complete'])\n", " \n", " # Create a figure with Plotly colorscale\n", " fig = create_gantt(df, colors='Blues', index_col='Complete',\n", " show_colorbar=True, bar_width=0.5,\n", " showgrid_x=True, showgrid_y=True)\n", " \n", " # Plot the data\n", " py.iplot(fig, filename='data with dataframe', world_readable=True)\n", " ```\n", "\n" ] } ], "source": [ "help(ff.create_gantt)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Collecting git+https://github.com/plotly/publisher.git\n", " Cloning https://github.com/plotly/publisher.git to /private/var/folders/tc/bs9g6vrd36q74m5t8h9cgphh0000gn/T/pip-YZa4vA-build\n", "Installing collected packages: publisher\n", " Found existing installation: publisher 0.10\n", " Uninstalling publisher-0.10:\n", " Successfully uninstalled publisher-0.10\n", " Running setup.py install for publisher ... \u001b[?25l-\b \bdone\n", "\u001b[?25hSuccessfully installed publisher-0.10\n" ] } ], "source": [ "from IPython.display import display, HTML\n", "\n", "display(HTML(''))\n", "display(HTML(''))\n", "\n", "! pip install git+https://github.com/plotly/publisher.git --upgrade\n", "import publisher\n", "publisher.publish(\n", " 'gantt.ipynb', 'python/gantt/', 'Python Gantt Charts | plotly',\n", " 'How to make Gantt Charts in Python with Plotly. Gantt Charts use horizontal bars to represent the start and end times of tasks.',\n", " title='Python Gantt Charts | plotly',\n", " name='Gantt Charts',\n", " thumbnail='thumbnail/gantt.jpg', language='python',\n", " has_thumbnail='true', display_as='basic', order=5.5,\n", " ipynb= '~notebook_demo/6')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "deletable": true, "editable": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.12" } }, "nbformat": 4, "nbformat_minor": 0 }