avatarMy Data Talk

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2040

Abstract

y</code> also has a lower-level graphing package called <code>Plotly Graph Objects</code>that generally requires a bit more coding but is more customizable and flexible compared to <code>Plotly Express</code>. In this post, we’ll use <code>Plotly Express</code> to plot the multi-layer Gantt chart.</p><p id="a7ec">If you don’t already have <code>plotly</code> on your computer, you can install it by running the following command:</p><div id="b378"><pre>pip <span class="hljs-keyword">install</span> plotly</pre></div><p id="4433">Let’s first import all the libraries and read our sample data into a Pandas dataframe. Notice that the data is structured in a way that for each task we have two rows of data, one for the planned start and finish dates and the other for the actual start and finish dates.</p> <figure id="61ee"> <div> <div>

            <iframe class="gist-iframe" src="/gist/insightsbees/c12e669bc8fc44519c8d90aa1b9a4db8.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><figure id="daa7"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*F-6Ria7o_zfMdUKNUtgOhQ.png"><figcaption>Image by Author</figcaption></figure><p id="ec66">To create a Gantt chart, we need to use <code>px.timeline()</code> to make a timeline plot where each row of the dataframe is represented as a rectangle/horizontal bar along the x-axis, spanning from the ‘Start’ date to the ‘Finish’ date, and each task is listed along the y-axis.</p><p id="f539">We also specify the <code>color</code> parameter to assign different colors to the ‘Planned’ and ‘Actual’ schedule bars. You can set your color preferences for the two bars in lines 2 and 3.</p>
    <figure id="8807">
        <div>
          <div>
            
            <iframe class="gist-iframe" src="/gist/insightsbees/530fa4241db91313793b7942e5034768.js" allowfullscreen="" frameborder="

Options

0" height="undefined" width="undefined"> </div> </div> </figure></iframe></div></div></figure><p id="b040">Now, this is the trick to make the Gantt chart look two-layered: we need to update the width of the ‘actual schedule’ bars so that they don’t completely overlap with the ‘planned schedule’ bars. We do it in line 16. Without applying this trick, your plot would look like the one on the upper left:</p><figure id="28d4"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*VrV95UgjktJCIYs8hjSMog.png"><figcaption></figcaption></figure><p id="78ac">We are almost done! Let’s fine-tune the chart by adding a chart title, moving the x-axis labels to the top, adjusting the position of the legend, removing the legend title, etc.</p> <figure id="c382"> <div> <div>

            <iframe class="gist-iframe" src="/gist/insightsbees/2a8cb1b4fd316e946d4e203cf59b0a21.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><figure id="f7cd"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*Yx8D7EncBhvvUm5YPj-M-w.png"><figcaption>Image by Author</figcaption></figure><p id="50a7">There you go! We have created a multi-layer Gantt chart that shows the project plan by planned and actual schedules using <code>Plotly.Express</code>. Thanks for reading and I hope you enjoyed this short tutorial about <code>Plotly</code>.</p><h2 id="b5c7">Data Source:</h2><p id="02be">The sample dataset used in this post was created by the author for demonstration purposes.</p><p id="a0e1">You can unlock full access to my writing and the rest of Medium by signing up for Medium membership ($5 per month) through this <a href="https://medium.com/@insightsbees/membership">referral link</a>. By signing up through this link, I will receive a portion of your membership fee at no additional cost to you. Thank you!</p></article></body>

How to Create a Multi-Layer Gantt Chart Using Plotly

Create a Gantt Chart to Track Your Project Plan by Planned vs. Actual Completion Dates

Image by Pixabay (Modified by Author)

A Gantt chart is a very popular and helpful visual for project managers and teams to plan and track project roadmap, schedules, and progress. A basic Gantt chart illustrates a project plan by listing the tasks to be performed on the vertical axis, and a timeline with planned start and finish dates for each task on the horizontal axis.

Sometimes in addition to showing the planned schedule for the tasks, we also want to show the actual start and completion dates for all the tasks. The Gantt chart below is a multi-layer Gantt chart that contains two layers for the planned and actual dates: the ‘planned schedule’ bars (green) are the first layer of the Gantt chart, and the ‘actual schedule’ bars (orange) are the second layer. How can we easily create a multi-layer Gantt chart like this using Python?

Image by Author

In this post, I’ll show you how to easily create a multi-layer Gantt chart using Plotly. For those of you who are not familiar with Plotly, the Plotly Python library is an interactive, open-source graphing library that covers a wide range of chart types and data visualization use cases. It has a wrapper called Plotly Express which is a higher-level interface to Plotly and is super easy and quick to use as a starting point for creating some of the most common visualizations.

Plotly also has a lower-level graphing package called Plotly Graph Objectsthat generally requires a bit more coding but is more customizable and flexible compared to Plotly Express. In this post, we’ll use Plotly Express to plot the multi-layer Gantt chart.

If you don’t already have plotly on your computer, you can install it by running the following command:

pip install plotly

Let’s first import all the libraries and read our sample data into a Pandas dataframe. Notice that the data is structured in a way that for each task we have two rows of data, one for the planned start and finish dates and the other for the actual start and finish dates.

Image by Author

To create a Gantt chart, we need to use px.timeline() to make a timeline plot where each row of the dataframe is represented as a rectangle/horizontal bar along the x-axis, spanning from the ‘Start’ date to the ‘Finish’ date, and each task is listed along the y-axis.

We also specify the color parameter to assign different colors to the ‘Planned’ and ‘Actual’ schedule bars. You can set your color preferences for the two bars in lines 2 and 3.

Now, this is the trick to make the Gantt chart look two-layered: we need to update the width of the ‘actual schedule’ bars so that they don’t completely overlap with the ‘planned schedule’ bars. We do it in line 16. Without applying this trick, your plot would look like the one on the upper left:

We are almost done! Let’s fine-tune the chart by adding a chart title, moving the x-axis labels to the top, adjusting the position of the legend, removing the legend title, etc.

Image by Author

There you go! We have created a multi-layer Gantt chart that shows the project plan by planned and actual schedules using Plotly.Express. Thanks for reading and I hope you enjoyed this short tutorial about Plotly.

Data Source:

The sample dataset used in this post was created by the author for demonstration purposes.

You can unlock full access to my writing and the rest of Medium by signing up for Medium membership ($5 per month) through this referral link. By signing up through this link, I will receive a portion of your membership fee at no additional cost to you. Thank you!

Plotly
Data Visualization
Data Science
Python
Recommended from ReadMedium