PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, June 28, 2022

[FIXED] How to plot timeline in a single bar?

 June 28, 2022     gantt-chart, graph, pandas, plotly, python     No comments   

Issue

I am trying to plot a timeline chart but they are stacking over each other.

import pandas as pd
import plotly.express as pex

d1 = dict(Start= '2021-10-10 02:00:00', Finish = '2021-10-10 09:00:00', Task = 'Sleep')
d2 = dict(Start= '2021-10-10 09:00:00', Finish = '2021-10-10 09:30:00', Task = 'EAT')
d3 = dict(Start= '2021-10-10 09:30:00', Finish = '2021-10-10 12:00:00', Task = 'Study')
d4 = dict(Start= '2021-10-10 12:00:00', Finish = '2021-10-10 16:00:00', Task = 'Work')
d5 = dict(Start= '2021-10-10 16:00:00', Finish = '2021-10-10 16:50:00', Task = 'EAT')
d6 = dict(Start= '2021-10-10 17:00:00', Finish = '2021-10-10 20:00:00', Task = 'Study')
d8 = dict(Start= '2021-10-10 20:00:00', Finish = '2021-10-10 20:40:00', Task = 'EAT')
d7 = dict(Start= '2021-10-10 21:00:00', Finish = '2021-10-11 05:00:00', Task = 'Sleep')

df = pd.DataFrame([d1,d2,d3,d4,d5,d6,d7,d8])

My DataFrame(df) is:

enter image description here

gantt = pex.timeline(df, x_start='Start', x_end = 'Finish', color = 'Task', height=300)
gantt.show()

enter image description here

This is the graph I am getting. But I don't want them to stack. I want them to be in a single line (There will not be any overlapping intervals). How do I achieve this?


Solution

According to the documentation, you can use the y parameter and provide an array_like object of size equal to the number of rows in df and all elements equal.

So, one way (using the empty string '' results in the plot having no y-axis range):

gantt = pex.timeline(df, x_start='Start', x_end = 'Finish', color = 'Task', height=300, y=['']*df.shape[0]) 


Answered By - Nikolaos Chatzis
Answer Checked By - Katrina (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing