11 views (last 30 days)

I have a two column vector containing x and y values. The y values have a high accuracy (many decimal points).

x ranges from 0 to 50000 and y from 0 to 14.

I need to use contourf to make a 2d representation of this data.

How can this be done?

Adam Danz
on 22 Jul 2020

Edited: Adam Danz
on 22 Jul 2020

The bivariate density can be computed using histcounts2. You'll need to specify either the number of bins or the bin edges for the x and y variables using one of the syntaxes below.

Here's what the solution will look like. xy is your nx2 matrix of [x,y] values.

[N,Xedges,Yedges] = histcounts2(xy(:,1), xy(:,2), 5);

% or use [N,Xedges,Yedges] = histcounts2(X,Y,Xedges,Yedges);

% Compute bin centers

Xcnt = Xedges(2:end) - abs(diff(Xedges(1:2))/2);

Ycnt = Yedges(2:end) - abs(diff(Yedges(1:2))/2);

figure()

contour(Xcnt,Ycnt, N)

% show bins

xlim = [min(Xedges),max(Xedges)];

ylim = [min(Yedges),max(Yedges)];

arrayfun(@(x)xline(x,'Color',[.8 .8 .8]),Xedges)

arrayfun(@(y)yline(y,'Color',[.8 .8 .8]),Yedges)

% colorbar

cb = colorbar();

ylabel(cb,'density')

KSSV
on 22 Jul 2020

I would follow like below:

- You need to generate z values which are density values.
- I would fix a distance r and calculate the number of points within r for each point using rangesearch.
- Now you can use scatter to plot the density values.
- If your data is structured arrange the data and use contourf.

Opportunities for recent engineering grads.

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

Start Hunting!
## 6 Comments

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/568329-making-a-contour-plot-from-x-and-y-data#comment_944535

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/568329-making-a-contour-plot-from-x-and-y-data#comment_944535

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/568329-making-a-contour-plot-from-x-and-y-data#comment_944538

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/568329-making-a-contour-plot-from-x-and-y-data#comment_944538

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/568329-making-a-contour-plot-from-x-and-y-data#comment_944556

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/568329-making-a-contour-plot-from-x-and-y-data#comment_944556

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/568329-making-a-contour-plot-from-x-and-y-data#comment_945411

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/568329-making-a-contour-plot-from-x-and-y-data#comment_945411

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/568329-making-a-contour-plot-from-x-and-y-data#comment_945417

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/568329-making-a-contour-plot-from-x-and-y-data#comment_945417

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/568329-making-a-contour-plot-from-x-and-y-data#comment_945423

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/568329-making-a-contour-plot-from-x-and-y-data#comment_945423

Sign in to comment.