Contour plot from x and y data points with corresponding contour level

I have sets of x and y data points that correspond to a specific contour level. For example (x1,y1) and (x2,y2) correspond to a contour level of 0.1 and (x3,y3) and (x4,y4) correspond to a contour level of 0.01. How do I plot these using the contour function?
In the attached .mat file, I have a cell array of tables. Each table has x and y data and the corresponding contour level (which is labeled Density). Basically, if you plot the x and y data using plot() it will create the contour, but I want to use contour() to plot to leverage labeling contours.

1 Comment

In the example for gridfit, I show how to recover a surface from a set of contours. (They actually came from a topographic map in the area of my home.) But you don't want to do that.
If all you want to do is show the contours, with labels on them, then just use plot, then use text to label the contour. There is no need to go through contour at all.

Sign in to comment.

Answers (1)

The code below plots the x,y coords of the data, in order to give insight get into the spatial sampling. The code makes 4 plots of all the data. The plots differ only in their axis limits. The 4 plots show that the x,y coordinates at each level differ by a factor of 10 or so from the next levels.
Matllab's contour() want data sampled on a rectangular grid. You could use interp2() to resample this data onto a rectangular grid, but the reuslts would not be very good, due to the different scales of spatial sampling at each level.
A=load('example');
d=[];
for i=1:length(A.T)
d=[d; table2array(A.T{i})];
end
Np=150; % number of points in each level
NL=length(A.T); % number of levels
colors=hsv2rgb([linspace(0,1,NL+1)',ones(NL+1,2)]); %
figure
subplot(221)
for i=1:NL
plot(d(Np*i-149:Np*i,1),d(Np*i-149:Np*i,2),'Marker','+','Color',colors(i,:));
hold on;
end
xlabel('X'); ylabel('Y'); grid on; legend()
subplot(222)
for i=1:NL
plot(d(Np*i-149:Np*i,1),d(Np*i-149:Np*i,2),'Marker','+','Color',colors(i,:));
hold on;
end
xlabel('X'); ylabel('Y'); grid on; xlim([0 15]); ylim([-10 10])
subplot(223)
for i=1:NL
plot(d(Np*i-149:Np*i,1),d(Np*i-149:Np*i,2),'Marker','+','Color',colors(i,:));
hold on;
end
xlabel('X'); ylabel('Y'); grid on; xlim([0 1.5]); ylim([-1 1])
subplot(224)
for i=1:NL
plot(d(Np*i-149:Np*i,1),d(Np*i-149:Np*i,2),'Marker','+','Color',colors(i,:));
hold on;
end
xlabel('X'); ylabel('Y'); grid on; xlim([0 .15]); ylim([-.1 .1])

4 Comments

The lines below compute and display the min and max values of x and y, and the z value, for each level.
The results show that the min and max x and y INcrease by sqrt(10) at each level, and the z value (density) DEcreases by a factor of exactly 10 with each level. Therefore this data is simulated, not measured from experiment, and density along the perimeter times area enclosed = constant. You could use only the outer ring of points (level 11) data to exactly predict the density for all other x,y points, including, but not limited to, the points on the other contour lines. You could fill in a grid if you wanted.
A=load('example');
NL=length(A.T); % number of levels=[];
for i=1:NL
d(:,:,i)=table2array(A.T{i});
end
xmin=min(squeeze(d(:,1,:))); xmax=max(squeeze(d(:,1,:)));
ymin=min(squeeze(d(:,2,:))); ymax=max(squeeze(d(:,2,:)));
z=mean(squeeze(d(:,3,:)));
for i=1:NL
fprintf('%.2e %.2e %.2e %.2e %.2e\n',xmin(i),xmax(i),ymin(i),ymax(i),z(i))
end
-8.21e-07 5.64e-03 -1.55e-03 1.55e-03 1.00e+00 -2.60e-06 1.78e-02 -4.90e-03 4.90e-03 1.00e-01 -8.21e-06 5.64e-02 -1.55e-02 1.55e-02 1.00e-02 -2.60e-05 1.78e-01 -4.90e-02 4.90e-02 1.00e-03 -8.21e-05 5.64e-01 -1.55e-01 1.55e-01 1.00e-04 -2.60e-04 1.78e+00 -4.90e-01 4.90e-01 1.00e-05 -8.21e-04 5.64e+00 -1.55e+00 1.55e+00 1.00e-06 -2.60e-03 1.78e+01 -4.90e+00 4.90e+00 1.00e-07 -8.21e-03 5.64e+01 -1.55e+01 1.55e+01 1.00e-08 -2.60e-02 1.78e+02 -4.90e+01 4.90e+01 1.00e-09 -8.21e-02 5.64e+02 -1.55e+02 1.55e+02 1.00e-10
Thanks for the thoughts. I would really want to use contour() such that I can get nice labels of each contour level and bd able to correlate to cdata, clim, etc. though. If the spatial domain is too big to use interpp2() or griddata(), this may prevent what I'm attempting to do.
@Chris Nemecek, I definitely agree with you that the capabilities and options available with contour() and related functions are very nice to be able to use.
What makes your data different from the image you provided in your recent comment is that your data spans ten orders of magnitude in z and 5 orders of magnitude in x and y. A common repsonse to that would be to use log scales. And we can do that for z, but not for x and y, since the x and y ranges include 0 and negative and positive values.
The step sizes between grid lines need not be uniform. This could be useful in your case.
@Chris Nemecek, this data reminds me of a convection-diffusion problem.

Sign in to comment.

Categories

Products

Release

R2023b

Asked:

on 31 May 2024

Commented:

on 2 Jun 2024

Community Treasure Hunt

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

Start Hunting!