You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多
文档控制台
免费开始使用

如何在Matlab中基于事件时间点向量绘制简易光栅图

嘿,刚好我之前也处理过类似的Matlab事件光栅图需求,给你分享几个简单好用的方法,完美适配你这种不同长度事件时间向量的场景~

单组事件轨迹的光栅图绘制

针对单个事件向量(比如你的V1),我们可以用向量化的方式快速画出所有垂直短划线,不用写繁琐的循环:

% 你的事件时间向量
V1 = [2 2.1 2.6 3.3 4 5 6 6.1 7];
y_pos = 1;          % 这组数据在y轴上的位置
line_len = 0.2;     % 垂直短划线的长度

% 构造绘图用的坐标矩阵
x_coords = repmat(V1, 2, 1);  % 把每个时间点复制成两行
y_coords = [y_pos - line_len/2; y_pos + line_len/2];  % 短划线的上下端点

% 绘制短划线
figure;
plot(x_coords, y_coords, 'k-', 'LineWidth', 1);

% 调整坐标轴和标签
xlim([0 10]);  % 匹配你说的10秒时长
ylim([y_pos - line_len, y_pos + line_len]);
xlabel('时间 (s)');
ylabel('事件轨迹');
title('单组事件光栅图');
box on;

这样就能得到你想要的一排垂直短划线效果,每个线对应V1里的一个事件时间点。

多组同步事件的光栅图绘制

对于多组不同长度的事件向量(比如V1V2),只需要给每组分配不同的y轴位置,重复上面的绘图逻辑即可:

% 你的两组事件时间向量
V1 = [2 2.1 2.6 3.3 4 5 6 6.1 7];
V2 = [1 5 7.1];
line_len = 0.2;

figure;
hold on;  % 保持画布,方便叠加绘制

% 绘制V1的短划线
y1 = 1;
x1 = repmat(V1, 2, 1);
y1_coords = [y1 - line_len/2; y1 + line_len/2];
plot(x1, y1_coords, 'k-', 'LineWidth', 1);

% 绘制V2的短划线
y2 = 2;
x2 = repmat(V2, 2, 1);
y2_coords = [y2 - line_len/2; y2 + line_len/2];
plot(x2, y2_coords, 'k-', 'LineWidth', 1);

% 调整坐标轴和标签
xlim([0 10]);
ylim([0.5 2.5]);
yticks([1 2]);
yticklabels({'V1', 'V2'});  % 给每组添加标签
xlabel('时间 (s)');
ylabel('事件组');
title('两组同步事件光栅图');
box on;
hold off;

这个代码跑出来的效果就和你描述的一致:V1是密集的短竖线,V2是稀疏的短竖线,各自在独立的y轴行上。

批量处理多组数据的小技巧

如果你有很多组事件轨迹,可以用循环来批量处理,不用重复写代码:

% 把所有事件向量放在cell数组里
event_groups = {V1, V2, [3, 4.5, 8, 9.2]};  % 可以任意添加新组
group_labels = {'V1', 'V2', 'V3'};  % 对应每组的标签
colors = {'#2c3e50', '#3498db', '#e74c3c'};  % 自定义每组的颜色
line_len = 0.2;

figure;
hold on;

% 循环绘制每组
for idx = 1:length(event_groups)
    current_V = event_groups{idx};
    y_pos = idx;
    x_coords = repmat(current_V, 2, 1);
    y_coords = [y_pos - line_len/2; y_pos + line_len/2];
    plot(x_coords, y_coords, colors{idx}, 'LineWidth', 1.2);
end

% 统一调整坐标轴
xlim([0 10]);
ylim([0.5 length(event_groups)+0.5]);
yticks(1:length(event_groups));
yticklabels(group_labels);
xlabel('时间 (s)');
ylabel('事件轨迹');
title('多组事件光栅图');
box on;
hold off;

这种方法不管你有多少组,只要把向量放进cell数组就行,非常灵活。

美化调整小建议

  • 如果你想让短划线更粗/更细,可以调整LineWidth参数;
  • 不同组用不同颜色可以更清晰区分,直接修改colors数组就行;
  • 如果想把短划线改成虚线,把'k-'改成'k--'即可;
  • 要是需要标记事件点的位置,可以在绘图后加一行scatter(V1, ones(size(V1)), 5, 'k', 'filled')来添加实心小点。

内容的提问来源于stack exchange,提问作者MatlabDumbDumb

火山引擎 最新活动