Clear Filters
Clear Filters

How to connect lines of a scatter plot?

2 views (last 30 days)
Samantha Horton
Samantha Horton on 12 Sep 2018
Answered: dpb on 12 Sep 2018
Hello, I am trying to connect points of a graph showing the average size of a QRS complex in a rat EKG. I have two sets of data, one called normal, one called psutre. Both sets of data have some 0 components, which I have set to NaN. When I plot this with a line plot, there are blank spaces. I want to connect the data points excluding NaN without changing the size of the array. Here is what I have:
load('rat data.txt');
normal=rat_data(:,1);
psutre=rat_data(:,2);
normal(normal==0)=NaN;
%idx=~any(isnan(normal),1);
len=1:length(normal);
figure
scatter(len,normal,'m')
hold
psutre(psutre==0)=NaN;
scatter(len,psutre,'g')
Let me know how to do this please!

Answers (1)

dpb
dpb on 12 Sep 2018
Well, the simple way is to do the plot of the data without the NaN elements; the builtin behavior is to ignore NaN and that's not changeable.
Try
...
normal(normal==0)=NaN;
idx=isfinite(normal);
len=1:length(normal);
figure
hLN=plot(len(idx),normal(idx),'m-o');
hold on
psutre(psutre==0)=NaN;
idx=isfinite(psutre);
hLP=line(len(idx),psutre(idx),'g-o')
This doesn't change the basic data arrays but only plots those that aren't NaN and won't have any breaks in the lines.
Use the line handles to modify the line properties to suit visual effect desired.

Community Treasure Hunt

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

Start Hunting!