You can plot both eyes on the same axes by splitting your original DataFrame by the eye
column.
eyemovementDF.groupby("eye")
to get two groups: “left” and “right.”px
(or py
) against timestamp
with a label.import matplotlib.pyplot as plt
create a new figure + axis
fig, ax = plt.subplots()
loop through left/right groups
for eye_label, df_eye in eyemovementDF.groupby("eye"):
# plot x‐coordinate
ax.plot(
df_eye["timestamp"],
df_eye["px"],
label=f"{eye_label.capitalize()} Eye – X",
linewidth=2
)
# plot y‐coordinate with a dashed line
ax.plot(
df_eye["timestamp"],
df_eye["py"],
label=f"{eye_label.capitalize()} Eye – Y",
linestyle="--"
)
labels and legend
ax.set_xlabel("Timestamp")
ax.set_ylabel("Pixel Position")
ax.legend()
plt.show()
If you prefer one wide DataFrame where left- and right-eye positions are side by side, use pivot
:
index="timestamp"
makes each timepoint a row.columns="eye"
creates two sub-columns (“left” and “right”).values=["px","py"]
fills the table with those measurements.df_pivot["px"]["left"]
and df_pivot["px"]["right"]
in one go.
import matplotlib.pyplot as plt
pivot the DataFrame
df_pivot = eyemovementDF.pivot(
index="timestamp",
columns="eye",
values=["px", "py"]
)
plot left vs. right X‐positions over time
fig, ax = plt.subplots()
ax.plot(
df_pivot.index,
df_pivot["px"]["left"],
label="Left Eye – X",
linewidth=2
)
ax.plot(
df_pivot.index,
df_pivot["px"]["right"],
label="Right Eye – X",
linewidth=2
)
ax.set_xlabel("Timestamp")
ax.set_ylabel("X Position")
ax.legend()
plt.show()
and similarly for Y if you wish
fig, ay = plt.subplots()
ay.plot(df_pivot.index, df_pivot["py"]["left"], label="Left – Y")
ay.plot(df_pivot.index, df_pivot["py"]["right"], label="Right – Y", linestyle="--")
ay.set_xlabel("Timestamp")
ay.set_ylabel("Y Position")
ay.legend()
plt.show()
Since you already have merge
from pd.merge(leftEye, rightEye, on="timestamp")
, you can plot its suffixed columns directly:
px_x
is the left-eye X (from leftEye
).px_y
is the right-eye X (from rightEye
).py_x
and py_y
.import matplotlib.pyplot as plt
fig, ax = plt.subplots()
left‐eye X vs right‐eye X
ax.plot(
merge["timestamp"],
merge["px_x"],
label="Left Eye – X",
linewidth=2
)
ax.plot(
merge["timestamp"],
merge["px_y"],
label="Right Eye – X",
linewidth=2
)
ax.set_xlabel("Timestamp")
ax.set_ylabel("X Position")
ax.legend()
plt.show()