0

I have following data frame with the country names as index and dates in 'start' and 'end' columns.

          start       end
Argentina   2021-01-31  2021-05-31
Australia   2021-02-28  2021-06-30
Brazil      2021-03-31  2021-07-31
Canada      2021-04-30  2021-08-31

I am looking for a way to plot the date range between the 'start' and 'end' dates for each of the countries like shown in the image below. Is there some way to do this in python? I would like country names from my data frame to appear where the crop names appear in the image below. And I don't care about the legend at the moment.

countries = ['Argentina', 'Australia', 'Brazil', 'Canada']

start = pd.date_range(start='2021-01', end ="2021-05", freq='M')

end = pd.date_range(start='2021-05', end ="2021-09", freq='M')

df= pd.DataFrame({'start':start,'end':end}, index=countries)

illustrative picture with different categories

1 Answers1

2

You can use plotly express timeline chart, which is usually the best way to get the 'Gantt chart' kind of chart. Code and graph below.

countries = ['Argentina', 'Australia', 'Brazil', 'Canada']
start = pd.date_range(start='2021-01', end ="2021-05", freq='M')
end = pd.date_range(start='2021-05', end ="2021-09", freq='M')
df= pd.DataFrame({'start':start,'end':end}, index=countries)

import plotly.express as px
import pandas as pd

df['country']=df.index
px.timeline(df,  y = 'country', x_start='start', x_end = 'end', color= 'country', color_discrete_sequence=px.colors.qualitative.D3)

enter image description here Output Graph

Redox
  • 9,321
  • 5
  • 9
  • 26
  • Thank you very much. This is what I was looking for and hadn't had experience using plotly before. – Dinesh Marimuthu May 17 '22 at 07:23
  • Forgot to mention... if you need to, you can have multiple colored bars in each line. An example is [here](https://stackoverflow.com/questions/68297565/how-to-enable-plotply-express-timeline-graphs-to-have-different-height-and-color) (check the first graph). Also, if you are fine, please close the question by clicking on the Green tick to accept the answer – Redox May 17 '22 at 08:13