MATLAB Answers

0

How to create curve in a plot scatter figure?

Asked by Dennis Huver on 30 Aug 2017
Latest activity Commented on by Dennis Huver on 31 Aug 2017
I am trying to create a curve in the plot, to show me density of the data(the dots). The curve needs to go up/down according to the scatter density, so that I can see where is most dense, where least and so on. I hope you get the picture. Thanks

  0 Comments

Sign in to comment.

2 Answers

Answer by Steven Lord
on 30 Aug 2017
 Accepted Answer

Use histcounts to count how much data is located in each bin along the X axis. Use the counts and edges in creating your plot. Alternately, use histogram with 'DisplayStyle', 'stairs'.

  3 Comments

How can you create a plot when the vectors are not the same length? Counts are 1 less than Edges.
Hey Steven, histogram thing worked. But is there a way to use curve instead of stairs? Thanks!
found a way from another answer. N = histcounts(X, edges); centers = (edges(1:end-1) + edges(2:end))/2; plot(centers, N) works like a charm. if there is more elegant way, let me know. THanks again for the tip! :)

Sign in to comment.


Answer by José-Luis
on 31 Aug 2017
Edited by José-Luis
on 31 Aug 2017

Convoluted way just to avoid repeating Steven's answer:
data = randn(5000,2); %First column xData, second column yData
[f,x] = ecdf(data(:,2));
[n,c] = ecdfhist(f,x,200);
x_val = linspace(min(data(:,1)),max(data(:,1)),200);
plot(data(:,1),data(:,2),'k.','LineWidth', 2);
hold on;
plot(c,n.*3,'r--')

  0 Comments

Sign in to comment.