# Struggling trying to create a 2-D contour plot from a large data set from x and y coordinates and corresponding frequency value

16 views (last 30 days)
Samuel Fraser on 26 May 2022
Answered: Bjorn Gustavsson on 27 May 2022
I am working with a very large data set of ~230k data points of x and y coordinates representing an s shaped tube and a corresponding frequency value for each coordinate. I am wanting to create a 2-D contour of this but have ran into issues.
I first tried using the contourf function using contourf(x,y,frequency) but matlab says that the frequency values need to be at least a 2x2 matrix. Each of my x, y and frequency data sets are in 230k x 1 doubles. Would anyone be able to advise how to go about rearanging my frequency values into at leat a 2x2 matrix while ensuring they still line up with their corresponding x and y coordinates.
The other method I had trouble with was turning the x and y coordinates into a grid using a mesh grid starting from the minimum values of x and y and increasing to the max by a step size determined by the range of the x and y values divided by 1000. I then fit the frequency values into this grid by using the griddata command using linear interpolation. This was a method shown to me that i use successfully on a smaller dataset of a circular cross section. When using this however it hasnt gone well, there appears to be some stretching of values. I will attach a photo of the result below as well as a plot of my x and y coordinates to show the domain i am working with as well the code used to get there. Can anyone advise on if my method is (in)correct or if my data set being so large is a contributing factor.
code used in second method:
[qx, qy] = meshgrid(min(x_halved):(max(x_halved)-min(x_halved))/1000:max(x_halved), min(y_halved):(max(y_halved)-min(y_halved))/1000:max(y_halved));
figure(1);
[qx, qy] = meshgrid(min(x):(max(x)-min(x))/1000:max(x), min(y):(max(y)-min(y))/1000:max(y));
figure(1);
qMode= griddata(x, y, max_freq_from_each_point_Ux, qx, qy, 'linear');
contourf(qx, qy, qMode, 'LevelStep',1,'LineStyle','none'); axis equal; set(gca,'FontSize',20);
colormap(jet); set(gca,'FontSize',20); colorbar('FontSize',20); caxis([0 1000]); xlabel('x axis','FontSize',12); ylabel('y axis','FontSize',12);
hColorbar = colorbar; set(hColorbar, 'Ticks', unique(sort([hColorbar.Limits, hColorbar.Ticks])));
Photo of resulting contour plot of second method:
photo of x and y coordinates showing domain
ignore the random x at ~(1,0) that isnt there

Bjorn Gustavsson on 27 May 2022
Wouldn't something like this do:
% Mock-up of data similar to yours...
x = linspace(-5,5,501);
y0 = linspace(-2,2,201);
[x,y0] = meshgrid(x,y0);
y = y0 - erf(x);
Z = peaks(500);
z = interp2(linspace(-5,5,500),linspace(-5,5,500),Z,x,y,'cubic');
% ...to here
% Then plain plotting of it down here:
If your data is very scattererd you might get some OK-ish display using scatter on the original data:
Psz = 12; % point-size
scatter(x, y, Psz,12,max_freq_from_each_point_Ux,'filled')
If you need to use griddata (or TriScatteredInterp) you might want to set all points outside your boundary to nan - for that you could use inpolygon to separate points inside your region-of-interest and the outside of the tube. Then that would remove the long triangles that fill out the convex polygon that the triangulation-functions make.
HTH

### Categories

Find more on Contour Plots in Help Center and File Exchange

R2021a

### Community Treasure Hunt

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

Start Hunting!

Translated by