import pandas as pd
import plotly.express as px
# Sample DataFrame
data = {
'START': ['2022-01-01 12:00:00', '2022-01-01 13:00:00', '2022-01-01 14:00:00', '2022-01-01 15:00:00'],
'FINISH': ['2022-01-01 12:30:00', '2022-01-01 13:30:00', '2022-01-01 14:30:00', '2022-01-01 15:30:00'],
'TASK': ['Task A', 'Task B', 'Task A', 'Task B'],
'Resource': ['Resource 1', 'Resource 2', 'Resource 1', 'Resource 2']
}
df = pd.DataFrame(data)
df['START'] = pd.to_datetime(df['START'])
df['FINISH'] = pd.to_datetime(df['FINISH'])
# Create an empty DataFrame to store the time ranges without Task A events
empty_ranges = pd.DataFrame(columns=['START', 'FINISH'])
# Find the time ranges where there's no Task A event
for i in range(len(df) - 1):
end_time = df.loc[i, 'FINISH']
start_time_next = df.loc[i + 1, 'START']
if df.loc[i, 'TASK'] == 'Task A' and df.loc[i + 1, 'TASK'] == 'Task A':
empty_ranges = empty_ranges.append({'START': end_time, 'FINISH': start_time_next}, ignore_index=True)
# Create a figure using px.timeline
fig = px.timeline(df, x_start='START', x_end='FINISH', y='TASK', color='Resource', title='Gantt Chart with Transparent Bar')
# Add transparent bars between Task A events
for _, row in empty_ranges.iterrows():
fig.add_shape(type='rect',
x0=row['START'], y0='Task A', x1=row['FINISH'], y1='Task A',
line=dict(color='rgba(0, 0, 0, 0)', width=0),
fillcolor='rgba(128, 128, 128, 0.2)') # Grey transparent color
# Show the figure
fig.show()
Hi,
I am trying to close the gap between Task A events with some transparent color to show it as inactive status of Task A event ⦠However, I cannot manage to add transparent bar between the event bars⦠May I know is there any way to close the gap between Task A events with transparent color bar? Thank you for your help in advanced.