I am looking to draw a line graph from a panda dataframe. Each line (3 point line) should be from each row and won’t have any connection with the next row’s data. I looked and couldn’t find how can I do it with plotly.
Just to provide detail:
Each row has say 4 columns for each event, where event starts but there is wait after that i.e. start_datetime, end_datetime, wait_duration, run_duration.
Datetime is in date time format and duration is in seconds.
I am looking to draw datetime on x-axis and duration on y -axis.
Need to draw 2 connecting lines of different color for each row i.e. first a vertical line - start_datetime to wait_duration. Then second line connecting from end of first line i.e. wait_duration to run_duration which should be finishing at end_datetime.
Same for each row.
Just wondering if something like this will be possible in plotly, I will highly appreciate if so some code snippet can be provided to solve this. Thanks
As you are adding traces manually, this can be very inefficient for large datasets. Besides, I am explicitly removing the traces from the legend (or there will be one for each legend).
Another approach would be to create all the red/blue lines together by “patching together” the coordinates for line in each row with None as separator. This is for the first (red) group of lines:
red_x = []
red_y = []
for idx, row in df.iterrows():
_x = [row["start_datetime"], row["start_datetime"]]
_y = [0, row["wait_duration"]]
red_x.append(_x[0])
red_x.append(_x[1])
red_y.append(_y[0])
red_y.append(_y[1])
red_x.append(None) # to separate lines, or they will be connected
red_y.append(None) # for each adjacent row
fig.add_trace(
go.Scatter(
x=red_x,
y=red_y,
mode="lines",
line={"color": "red"}
)
)