我有一个由四列组成的眼球跟踪数据集:

  • t是时间戳的数字数组
  • xy是凝视的像素坐标
  • e是将每个样本标记为不同凝视事件(凝视、扫视等)的值{0, 1, 2, 3, 4, 5}的数组

我想绘制xy随时间变化的坐标,并在图上/图下添加一个矩形,该矩形的 colored颜色 根据值e而变化.

以下是一些示例数据:

t = np.arange(30)
x = np.array([125.9529, 124.6142, 125.0569, 125.3117, 126.7498, 127.035,125.4822, 125.6249, 126.9371, 127.6047, 129.031 , 128.2419, 121.521 , 114.7071, 109.4141, 100.5057,  94.9606,  95.2231, 95.9032,  96.4991, 101.2602, 103.9582, 108.2527, 108.8801, 110.3254, 112.8205, 113.0079, 113.3547, 113.0962, 113.2508])
y = np.array([31.218 , 31.236 , 31.147 , 31.2614, 30.806 , 30.8423, 31.727, 32.2256, 32.0504, 32.7774, 34.7089, 37.0671, 46.309 , 55.9716, 62.4481, 68.0248, 75.4912, 79.0622, 81.2176, 83.191 , 83.7656, 84.6713, 83.9343, 82.4546, 81.1652, 80.7981, 80.2136, 80.7405, 80.4398, 80.0738])
e = np.array([1., 1., 1., 1., 1., 1., 1., 1., 1., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 3., 3., 3., 3., 3., 3., 4., 4., 4., 4.])

而我的编码try 是:

import plotly.graph_objects as go
from plotly.subplots import make_subplots

fig = make_subplots()
fig.add_trace(ply.graph_objects.Line(x=t, y=x, name="X"),
              secondary_y=False, row=1, col=1)
fig.add_trace(ply.graph_objects.Line(x=t, y=y, name="Y"),
              secondary_y=False, row=1, col=1)
fig.add_shape(type="rect",
              x0=t[0], y0=0, x1=t[-1], y1=0.05 * np.max([x, y]),
              line=dict(color="black", width=2),
              fillcolor=e)

这就提高了Value Error: Invalid value of type 'numpy.ndarray' received for the 'fillcolor' property of layout.shape

推荐答案

继《S answer》之后,我在这里附上了完整的代码,供后人参考:

# create discrete color scale:
colors = list('rgbako')
possible_events = np.arange(6).astype(int)
bounds = sorted(np.concatenate([possible_events, [len(possible_events)]]))
norm_bounds = [(b - bounds[0]) / (bound[-1] - bounds[0]) for b in bounds]

d_colors = []
for k in range(len(colors)):
  discrete_colors.extend([(norm_bounds[k], colors[k]), (norm_bounds[k+1], colors[k+1])])

# assume you have t, x, y, e
# create a figure with X- Y-coordinates over time, and overlay the event chart
fig = make_subplots(specs=[[{"secondary_y": True}]])
fig.add_trace(go.Scatter(x=t, y=x, mode="lines", line=dict(color='#ff0000', width=4), name="X"), secondary_y=False)
fig.add_trace(go.Scatter(x=t, y=y, mode="lines", line=dict(color='#0000ff', width=4), name="Y"), secondary_y=False)

# add events as heatmap
fig.add_trace(go.Heatmap(z=[e], zmin=np.nanmin(possible_events), zmax=np.nanmax(possible_events),
                         x=t, y=[0, 0.5 * np.nanmin([x, y])],
                         colorscale=d_colors,
                         colorbar=dict(
                             len=0.5,
                             thickness=25,
                             tickvals=[np.mean(possible_events[k:k+2]) for k in range(len(possible_events)-1)],
                             ticktext=[e for e in possible_events]
                         )),
              secondary_y=False)

# move legend to top left
fig.update_layout(legend=dict(
    yanchor="top",
    y=0.99,
    xanchor="left",
    x=0.01
))

Here's an example output (there's also a velocity v trace there, can ignore it): enter image description here

Python相关问答推荐

根据另一列中的nan重置值后重新加权Pandas列

将jit与numpy linSpace函数一起使用时出错

PywinAuto在Windows 11上引发了Memory错误,但在Windows 10上未引发

如何删除索引过go 的lexsort深度可能会影响性能?' &>

. str.替换pandas.series的方法未按预期工作

pandas滚动和窗口中有效观察的最大数量

从spaCy的句子中提取日期

Pandas—在数据透视表中占总数的百分比

如何在图中标记平均点?

当我try 在django中更新模型时,模型表单数据不可见

使用Python查找、替换和调整PDF中的图像'

dask无groupby(ddf. agg([min,max])?''''

Python—压缩叶 map html作为邮箱附件并通过sendgrid发送

在Python中从嵌套的for循环中获取插值

裁剪数字.nd数组引发-ValueError:无法将空图像写入JPEG

在第一次调用时使用不同行为的re. sub的最佳方式

用来自另一个数据框的列特定标量划分Polars数据框中的每一列,

如何将一个文件的多列导入到Python中的同一数组中?

对于数组中的所有元素,Pandas SELECT行都具有值

有没有一种方法可以根据不同索引集的数组从2D数组的对称子矩阵高效地构造3D数组?