MATLAB Answers

0

Plotting index-determined points in a continuous line plot in a different color

Asked by Tom Andersson on 28 Jul 2017
Latest activity Commented on by José-Luis
on 8 Aug 2017
I am investigating spike detection in audio data. I have 3 vectors of data: t, xp and yp, which I produce a 3D diagram from using plot3. Earlier on in my script, I determine a vector, say 'color2', containing the indexes of points that I would like to plot as blue, with the rest of the data as black. It is easy to produce a 3D plot of un-connected points with the following code:
plot3(fig4,t(color1),xp(color1),yp(insideIndex),'k.'); % Plot points as black dots
plot3(fig4,t(color2),xp(color2),yp(color2),'bo'); % Plot points of interest as blue pluses
This produces the following plot:
However, ideally I would like a single, continuous line of data where blue points are joined by blue lines and black points are joined by black lines, similar to the image below:
What's the simplest way of doing this? Cheers.

  0 Comments

Sign in to comment.

1 Answer

Answer by Saurabh Gupta on 31 Jul 2017

Generally speaking, line plot functions combine adjoining points, based on the input vectors, into line segments. All you need to do it separate the data for both cases, and arrange the points in each data set in order of succession (as per line plotting requirements). Then, the 'plot3' command should be able to produce the continuous line plots that you require.
I am guessing that you are aware that your current commands use LineSpec as 'k.' and 'bo' which plot points only and not the connecting lines. But I thought I should mention it also just in case.
Hope this helps!

  4 Comments

Show 1 older comment
I'm sorry, I should have been clearer. I will explain as follows.
Lets take an example of the following data points.
>> points1 = rand(4,3);
>> points2 = rand(4,3);
You are currently plotting them as follows:
>> plot3(ax, points1(:,1), points1(:,2), points1(:,3), 'k.');
>> plot3(ax, points2(:,1), points2(:,2), points2(:,3), 'bo');
To connect the lines, all you have to change is the following
>> plot3(ax, points1(:,1), points1(:,2), points1(:,3), '-k.');
>> plot3(ax, points2(:,1), points2(:,2), points2(:,3), '-bo');
Notice that I only changed 'k.' to '-k.' and 'bo' to '-bo'. If you just care about getting 2 continuous lines connecting the two sets of points, then the solution would be as simple as this.
But if you want the connections to be in a specific order, then you may have to re-order your points.
For example, in the below data for the above plot, the point (0.5277, 0.4981, 0.7386) is connected to the point (0.4795, 0.9009, 0.5860), and so on.
>> points1
points1 =
0.5277 0.4981 0.7386
0.4795 0.9009 0.5860
0.8013 0.5747 0.2467
0.2278 0.8452 0.6664
If you want the plot to connect the point (0.5277, 0.4981, 0.7386) to the point (0.8013, 0.5747, 0.2467), for instance, then you will firstly need to modify the ordering of the points in the 'points1' array and make sure that the point (0.5277, 0.4981, 0.7386) is adjacent to (0.8013, 0.5747, 0.2467).
I hope this clarifies!
Thank you for the clarification Saurabh - your explanation has made me realise that I wasn't clear enough in my question. I don't want two separate lines of different colors, I want one continuous line comprised of two different colors. I don't think MATLAB supports this functionality, but perhaps there is a workaround?
I'm afraid it's not going to happen automagically. You will need two lines if you want two different colors.

Sign in to comment.