I am trying to create a Python Plotly go.scatter plot with multiple rectangles without a loop. A loop would be fine if my hover labels would plot correctly. I created X and Y columns using the dates and Y values that will trace these rectangles. I would like them to have a hover label with the custom data. I’ve been working on various ways to accomplish this but cant seem to figure it out.
This code below doesn’t work (the rectangles plot on top of each other):
df = pd.DataFrame(columns=['description','date_begin','date_end','y0','y1'])
df.loc[0] = ['red', '2022-09-24 08:22:31', '2022-09-24 20:40:00', 0, 1]
df.loc[1] = ['blue', '2022-10-09 02:54:56', '2022-10-10 19:09:40', 0, 1]
df.loc[2] = ['green', '2022-11-03 06:21:51', '2022-11-05 15:15:48', 0, 1]
df['date_begin'] = pd.to_datetime(df['date_begin'])
df['date_end'] = pd.to_datetime(df['date_end'])
df['x'] = df.apply(lambda x: [x['date_begin'], x['date_begin'], x['date_end'], x['date_end'], x['date_begin']], axis=1)
df['y'] = df.apply(lambda x: [x['y1'], x['y0'], x['y0'], x['y1'], x['y1']], axis=1)
fig = go.Figure()
fig.add_traces(go.Scatter( x=df['x'],
y=df['y'],
mode='lines+markers',
customdata=df[['description','date_begin','date_end','y0','y1']],
hovertemplate="<br>".join(["Description: %{customdata[0]}",
"Beginning date: %{customdata[1]}",
"Ending date: %{customdata[2]}",
'<extra></extra>']),
line=dict(color="black",width=1),
fillcolor="grey",
opacity=0.5,
fill="toself",
))
fig.show()
When I just use one row, the rectangle plots as expected but the hover labels aren’t working:
df = pd.DataFrame(columns=['description','date_begin','date_end','y0','y1'])
df.loc[0] = ['red', '2022-09-24 08:22:31', '2022-09-24 20:40:00', 0, 1]
df.loc[1] = ['blue', '2022-10-09 02:54:56', '2022-10-10 19:09:40', 0, 1]
df.loc[2] = ['green', '2022-11-03 06:21:51', '2022-11-05 15:15:48', 0, 1]
df['date_begin'] = pd.to_datetime(df['date_begin'])
df['date_end'] = pd.to_datetime(df['date_end'])
df['x'] = df.apply(lambda x: [x['date_begin'], x['date_begin'], x['date_end'], x['date_end'], x['date_begin']], axis=1)
df['y'] = df.apply(lambda x: [x['y1'], x['y0'], x['y0'], x['y1'], x['y1']], axis=1)
fig = go.Figure()
fig.add_traces(go.Scatter( x=df['x'].iloc[0],
y=df['y'].iloc[0],
mode='lines+markers',
customdata=df[['description','date_begin','date_end','y0','y1']].iloc[[0]],
hovertemplate="<br>".join(["Description: %{customdata[0]}",
"Beginning date: %{customdata[1]}",
"Ending date: %{customdata[2]}",
'<extra></extra>']),
line=dict(color="black",width=1),
fillcolor="grey",
opacity=0.5,
fill="toself",
))
fig.show()
If I create a loop, the rectangles plot correctly but the hover labels aren’t correct:
df = pd.DataFrame(columns=['description','date_begin','date_end','y0','y1'])
df.loc[0] = ['red', '2022-09-24 08:22:31', '2022-09-24 20:40:00', 0, 1]
df.loc[1] = ['blue', '2022-10-09 02:54:56', '2022-10-10 19:09:40', 0, 1]
df.loc[2] = ['green', '2022-11-03 06:21:51', '2022-11-05 15:15:48', 0, 1]
df['date_begin'] = pd.to_datetime(df['date_begin'])
df['date_end'] = pd.to_datetime(df['date_end'])
df['x'] = df.apply(lambda x: [x['date_begin'], x['date_begin'], x['date_end'], x['date_end'], x['date_begin']], axis=1)
df['y'] = df.apply(lambda x: [x['y1'], x['y0'], x['y0'], x['y1'], x['y1']], axis=1)
fig = go.Figure()
for i in range(df.shape[0]):
fig.add_traces(go.Scatter( x=df['x'].iloc[i],
y=df['y'].iloc[i],
mode='lines+markers',
customdata=df[['description','date_begin','date_end','y0','y1']].iloc[i],
hovertemplate="<br>".join(["Description: %{customdata[0]}",
"Beginning date: %{customdata[1]}",
"Ending date: %{customdata[2]}",
'<extra></extra>']),
line=dict(color="black",width=1),
fillcolor="grey",
opacity=0.5,
fill="toself",
))
fig.show()
p.s. This is my first post and I don’t know how to embed the python code in this post