Connecting points of a scatter plot with a line

I am using the plotSpread function to to plot my data with categorical x axis (fig(1)). In order to see the changes of a point in the plot from the first stage to the next (NREM to Wake), I want to connect them with a line.
How can I plot the lines such that they connect all points in the two clusters? The jitter causes only the y axis coordinates to be connected, and not the actual points. I use the following loop to plot this:
%Plotting the data
nrem_rgs=Pyr_RGS_Unit_Wise_Data(:,3);
wake_rgs=Pyr_RGS_Unit_Wise_Data(:,6);
both=[nrem_rgs wake_rgs];
plotSpread(both,'xNames',{'NREM','Wake'},'distributionColors',{[0.4 .4 1],[0.2 0.2 .6]}, ...
'yLabel','Firing Rate Across Study Day')
% Loop for line connections
for k = 1 : size(nrem_rgs, 1)
plot([nrem_rgs(k), wake_rgs(k)],...
'rs-', 'MarkerSize', 5);
hold on
end
nrem_rgs and wake_rgs are of size (87,1)
I have read similiar questions but their solutions don't seem to work well for me.Thank you for your help!

5 Comments

Not sure exactly what you're wanting, but if you want the actual points as plotted including where they were placed on the axes by the jitter introduced, then you'll have to retrieve the X|YData from the figure and use those points to connect the lines.
I've not downloaded the FEX submission to look at it, it might (probably would?) be pretty simple to just edit it to change the line style to use a line as well as the marker.
Like @dpb, I'm not sure what you want since you didn't actually ask a question. No sentence ends with a question mark, and I can't even guess what your question would be. It sounds like you are doing what you want (plotting a line from each point in one data set to the corresponding point in the other data set), so what's the problem? If you want to read over the posting guidelines again to better prepare your posting as a question that might actually get a useful answer, see
Otherwise I guess all I can say is "thanks for the announcement".
Hi, thanks for your comment. I guess you're right. What I should've made clear is that there is a jitter in the points I plotted, and if you see the second plot it appears as if not all of them are connected to each other, and that's something I wanted to fix.
So, save the line handles (if the function returns them) when you plot (or retrieve them from the axes 'Children' property if it doesn't) and read those and use them instead of the raw data.
Or, if the function will return the jittered data as well, save and use it when create the plot first time.
I presume you would get a unique set every time you execute the function (that it doesn't internally reset the seed to be reproducible).
Thank you, this did the trick.

Sign in to comment.

Answers (0)

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!