使用matlab绘制不同颜色的选定索引

时间:2015-08-15 19:46:09

标签: matlab plot colors subplot

我正在尝试绘制错误信号。当误差为零时,绘图应为黑色。每当出现错误时,它应该是红色。我在数组excdIdx中捕获带有错误的索引。 我的目标是将红色和非错误索引中的错误索引绘制为黑色。由于某种原因,for循环不起作用。

ErrAxis是轴 excdIdx包含要绘制inred的索引列表

    h(8) = subplot(plotCount,1,ErrAxis);
    hold off;
    if excdIdx > 0

        plot(time(1:excdIdx),Output(1:excdIdx) - trueSignal(1:excdIdx),'k-'); hold on;

       %this method to plot the error indices in red is not working
       % for elem = 1: size(excdIdx, 2)
       %     index_1 = excdIdx(elem);
       %     index_2 = excdIdx(elem+1);
       %     plot(time(index_1:index_1+1),Output(index_1:index_1+1) - trueSignal(index_1:index_1+1),'r-'); hold on;
       % end 

    else
        plot(time,Output - trueSignal,'k-'); hold on;
    end

1 个答案:

答案 0 :(得分:0)

如果您正在绘制单个数据点,我认为您应该使用scatter。请尝试以下代码:

h(8) = subplot(plotCount,1,ErrAxis);
hold off;
if excdIdx > 0
    scatter(time(1:excdIdx),Output(1:excdIdx) - trueSignal(1:excdIdx),'markerfacecolor','k'); hold on;
    for elem = 1: size(excdIdx, 2)
        index_1 = excdIdx(elem);
        index_2 = excdIdx(elem+1);
        scatter(time(index_1:index_1+1),Output(index_1:index_1+1) - trueSignal(index_1:index_1+1),'markerfacecolor','r');
    end
else 
    scatter(time,Output - trueSignal,'markerfacecolor','k'); hold on;
end