Change color of 2D plot line depending on 3rd value

前端 未结 5 1977
Happy的楠姐
Happy的楠姐 2020-11-30 10:40

I have a data set that looks like this

 140400 70.7850 1
 140401 70.7923 2
 140402 70.7993 3
 140403 70.8067 4
 140404 70.8139 5
 140405 70.8212 3

5条回答
  •  一向
    一向 (楼主)
    2020-11-30 11:03

    My desired effect was achieved below (simplified):

            indices(1).index  = find( data( 1 : end - 1, 3) == 1);
            indices(1).color  = [1 0 0]; 
            indices(2).index  = find( data( 1 : end - 1, 3) == 2 | ...
                                      data( 1 : end - 1, 3) == 3);
            indices(2).color  = [1 1 0];
            indices(3).index  = find( data( 1 : end - 1, 3) == 4 | ...
                                      data( 1 : end - 1, 3) == 5);
            indices(3).color  = [0 1 0];
            indices(4).index  = find( data( 1 : end - 1, 3) == 10);
            indices(4).color  = [0 0 0];
            indices(5).index  = find( data( 1 : end - 1, 3) == 15);
            indices(5).color  = [0 0 1];
    
        % Loop through the locations of the values and plot their data points
        % together (This will save time vs. plotting each line segment
        % individually.)
    
        for iii = 1 : size(indices,2)
    
            % Store locations of the value we are looking to plot
            curindex = indices(iii).index;
    
            % Get color that corresponds to that value
            color = indices(iii).color;
    
                % Create X and Y that will go into plot, This will make the line
                % segment from P1 to P2 have the color that corresponds with P1
                x = [data(curindex, 1), data(curindex + 1, 1)]';
                y = [data(curindex, 2), data(curindex + 1, 2)]';
    
                % Plot the line segments
                hold on
                plot(x,y,'Color',color,'LineWidth',lineWidth1)            
    
        end
    

提交回复
热议问题