Issue
Currently, my data is in a nested list as [[ID1, Pos1, Time1], [ID2, Pos2, Time2], [ID1, Pos3, Time3]....].
I then put that into a Pandas dataframe. I want to graph position (Pos values) against time for each object ID separately in Plotly, and my searches of plotly documentation haven't helped. What I'd like is for each object ID to have a separate Pos vs Time line plot on the same figure.
How would I do this? I'm unsure whether I should reshape my data first so that repeated ID values are grouped somehow or whether I can do this direct from the plotly plotting function. I'm new at this, so any help would be greatly appreciated.
Here's the code snippet:
dataset=[]
for obj in scene.objects:
obj_data = [obj.ID, obj.time, obj.pos]
dataset.append(obj_data)
df = pd.DataFrame(data=dataset, columns=["ID", "Time", "Pos"])
Also, the output of the first five entries of the dataframe:
ID Time ... Speed
0 4 0.0 ... 1.465062
1 5 0.0 ... 1.195697
2 6 0.0 ... 1.443732
3 7 0.0 ... 1.318886
4 8 0.0 ... 1.258748
Solution
You could group the data in your dataframe by "ID" and then add a line plot to the same figure:
import plotly.graph_objs as go
fig = go.Figure() # Same figure object
for group_name, group_df in df.groupby(["ID"]):
# group_name is the "ID" value
# group_df is a dataframe with the rows of each "ID" value
fig.add_trace(
go.Scatter( # This is how we draw a line plot
x=group_df["Time"], y=group_df["Pos"],
name=group_name, # Add the "ID" as the name of the line
)
)
fig.show() # Display figure
This is a sample input and expected output using this code:
ID Pos Time
0 1 1 1
1 1 3 2
2 0 2 1
3 0 4 2
Answered By - aaossa
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.