How to plot contour of an xy plot with a constant value?

I have several data sets (I have selected two lines in this case. I want to plot them as contour lines with different z values. For example, I want to plot the xy-data with z values as 40 and 30. As the lines extend beyond my area of interest, the current are of interest is 0 to 120 in x and 0 to 220 in y axis. I tried using meshgrid which I have used for plotting contour plots but I cannot convert these known values of lines into contours.Your help will be much appreciated.
xy = readtable('xy_data.xlsx');
x1 = xy.x40;
y1 = xy.y40;
x2 = xy.x30;
y2 = xy.y30;
figure
rectangle('Position',[0 0 120 220]) % the contour line can be only drawn in this box
axis equal
hold on
plot(x1,y1)
plot(x2,y2)

 Accepted Answer

hello
why not simply use xlim and ylim to plot the required area ?
xy = readtable('xy_data.xlsx');
x1 = xy.x40;
y1 = xy.y40;
x2 = xy.x30;
y2 = xy.y30;
figure
% rectangle('Position',[0 0 120 220]) % the contour line can be only drawn in this box
axis equal
hold on
plot(x1,y1)
plot(x2,y2)
xlim([0 120])
ylim([0 220])

11 Comments

Thank you for your response @Mathieu. Let me clear it, I want to draw a contour line using either contour or contourf plot. The resulting plot will be a 2d contour with these two lines at the respective z values of 40 and 30.
ok so why not work directly on the relevant data and code ?
Dear @Mathieu NOE. I am trying to incorporate into my existing code where I want to linearly interpolate the contours to find intemediate contour line. I have to make comparison with another method for which I have to sort of inverse plot the contour i.e. use xy-coordinates to draw contours.
I am not sure why you need to interpolate between contours ,as you can specify at which z value (threshold) you want the contour to be generated , and this can be any value in the valid range of Z data
example
[X,Y,Z] = peaks(25);
figure(1),hold on
s = surf(X,Y,Z,'Facealpha',0.75,'Edgecolor','none');
view(2);
colorbar('vert');
threshold = 0.3*max(Z,[],'all');
[c,h] = contour(X,Y,Z,[threshold threshold],'-k','Linewidth',5);
hold off
Let me explain in detail why I am requesting a specific solution.
I have 2 sets of data. One is a contour map which has been plotted using `griddata` and 'contourf`, as can be seen in figure as solid lines with the corresponding contour values. The other data set is reference data, as can be seen by dotted lines which roughly follow the solid lines, as can be seen in the figure below.
The reference data are availabe in the form of an xy dataset instead of contour matrices (attached in this answer as ref_data.mat).
Now, coming back to the contour data (shown by solid lines). I have further used these data to interpolate to find a desired value (say 12), as shown by red line in the figure below.
This line I later extract the x-y corrdinates to further process as xy data.
Now, within the scope of my question, I have the reference data as xy coordinates (attached as ref_data.mat). And I have plotted it below.
load('ref_data.mat');
ref = table2array(reference_data);
figure; hold on; axis equal; box on; grid on
for i = 1:2:21
x = ref(:,i);
y = ref(:,i+1);
plot(x,y,'--')
end
Now I have x-y lines, which I cannot transform into an equivalent contour plot so that i can again apply the subsequent interpolation to get the contour line at 12. As you can see, the reference data and the contour data overlap. I want to obtain the corresponding interpolated lines from both these data sets and compare. And, for this reason, I cannot transform the reference data into an equivalent representation of contour map which I intend to use conveniently for subsequent interpolations. I hope I have explained my problem in detail. I am sorry for inconvenience caused to you.
try this
I modified a bit your code so that your ref data have same amount of data for all levels , it ùakes things simpler afterwards for linear interpolation between levels
I created three new levels as you can see below (in red)
load('ref_data.mat');
ref = table2array(reference_data);
figure; hold on; axis equal; box on; grid on
k = 0;
[m,n] = size(ref);
x_all = [];
y_all = [];
for i = 1:2:n-1
x = ref(:,i);
y = ref(:,i+1);
% remove NaN values
id = isnan(x) & isnan(y);
x(id) = [];
y(id) = [];
if ~isempty(x) % this loop must be executed on non empty data
k = k+1;
levels(k) = 30-5*(i-1); % just to have some levels values
nx = numel(x);
ind=linspace(1,nx,m);
xi = interp1(1:nx,x,ind);
yi = interp1(1:nx,y,ind);
x_all = [x_all xi(:)];
y_all = [y_all yi(:)];
plot(x,y,'*',xi,yi,'-')
text(x(end)+5,y(end),num2str(levels(k)));
end
end
% create som interpolated level lines
newlevel = [15 3 -45];
new_x = zeros(m,numel(newlevel));
new_y = zeros(m,numel(newlevel));
for ck =1:numel(newlevel)
ind = interp1(levels,1:numel(levels),newlevel(ck));
if ~isnan(ind) % this loop must be executed on valid (in range) new levels
ind_low = floor(ind);
ratio = ind-ind_low;
ind_up = ind_low+1;
new_x(:,ck) = (1-ratio)*x_all(:,ind_low) + ratio*x_all(:,ind_up);
new_y(:,ck) = (1-ratio)*y_all(:,ind_low) + ratio*y_all(:,ind_up);
plot(new_x(:,ck),new_y(:,ck),'r')
text(new_x(end,ck)+5,new_y(end,ck),num2str(newlevel(ck)));
end
end
Well this works perfectly for my code. Thank you so much. I will incorporate it in my analysis. It is very easy to follow and simplistic approach, exactly what I wanted to do.
Thank you so much for your precious time.
Cheers~
I am sorry to disturb again. I incorporated the code into my code by simply reducing the number of loops to 1 for interpolation. In addition I generalized for the reference table range which values were from -50 to 50. However, in some instances it does not interpolate. I have looked into how upper and lower bounds are established and then the ratios are multiplied and added in lower bounds, it should work good but still I cannot figure out the problem with interpolation. I shared the code as it is below. The interpolation in this instance gives odd values. Thank you
ref = table2array(readtable('table.xlsx'));
figure; hold on; axis equal; box on; grid on
k = 0;
[m,n] = size(ref);
x_all = [];
y_all = [];
for i = 1:2:n-1
x = ref(:,i);
y = ref(:,i+1);
% remove NaN values
id = isnan(x) & isnan(y);
x(id) = [];
y(id) = [];
if ~isempty(x) % this loop must be executed on non empty data
k = k+1;
levels(k) = 50-5*(i-1); % just to have some levels values
nx = numel(x);
ind=linspace(1,nx,m);
xi = interp1(1:nx,x,ind);
yi = interp1(1:nx,y,ind);
x_all = [x_all xi(:)];
y_all = [y_all yi(:)];
plot(x,y,'*',xi,yi,'-')
text(x(end)+5,y(end),num2str(levels(k)));
end
end
% create som interpolated level lines
newlevel = [-12.7];
new_x = zeros(m,numel(newlevel));
new_y = zeros(m,numel(newlevel));
for ck =1:numel(newlevel)
ind = interp1(levels,1:numel(levels),newlevel(ck));
if ~isnan(ind) % this loop must be executed on valid (in range) new levels
ind_low = floor(ind);
ratio = ind-ind_low;
ind_up = ind_low+1;
new_x(:,ck) = (1-ratio)*x_all(:,ind_low) + ratio*x_all(:,ind_up);
new_y(:,ck) = (1-ratio)*y_all(:,ind_low) + ratio*y_all(:,ind_up);
plot(new_x(:,ck),new_y(:,ck),'r')
text(new_x(end,ck)+5,new_y(end,ck),num2str(newlevel(ck)));
end
end
ok problem fixed - the picture you posted pointed me to the issue , simply because your new data has sometimes the y sorted downwards , and my first code supposed that y was always in ascending order
so the fix was quite simple , always check y data and flip it if needed
it works better now (tested also with your first data )
% ref = table2array(readtable('table.xlsx'));
ref = readmatrix('table.xlsx'); % simpler and faster
figure; hold on; axis equal; box on; grid on
k = 0;
[m,n] = size(ref);
x_all = [];
y_all = [];
for i = 1:2:n-1
x = ref(:,i);
y = ref(:,i+1);
% remove NaN values
id = isnan(x) & isnan(y);
x(id) = [];
y(id) = [];
if ~isempty(x) % this loop must be executed on non empty data
k = k+1;
levels(k) = 50-5*(i-1); % just to have some levels values
nx = numel(x);
% let's check if y is always in ascending order (may not be the case and that must be corrected)
ysign = sign(mean(diff(y)));
% correct if needed
if ysign<0
x = flip(x);
y = flip(y);
end
ind=linspace(1,nx,m);
xi = interp1(1:nx,x,ind);
yi = interp1(1:nx,y,ind);
x_all = [x_all xi(:)];
y_all = [y_all yi(:)];
plot(x,y,'*',xi,yi,'-')
text(x(end)+5,y(end),num2str(levels(k)));
end
end
% create som interpolated level lines
newlevel = [-12.7];
new_x = zeros(m,numel(newlevel));
new_y = zeros(m,numel(newlevel));
for ck =1:numel(newlevel)
ind = interp1(levels,1:numel(levels),newlevel(ck));
if ~isnan(ind) % this loop must be executed on valid (in range) new levels
ind_low = floor(ind);
ratio = ind-ind_low;
ind_up = ind_low+1;
new_x(:,ck) = (1-ratio)*x_all(:,ind_low) + ratio*x_all(:,ind_up);
new_y(:,ck) = (1-ratio)*y_all(:,ind_low) + ratio*y_all(:,ind_up);
plot(new_x(:,ck),new_y(:,ck),'r')
text(new_x(end,ck)+5,new_y(end,ck),num2str(newlevel(ck)));
end
end
Thank you. It works perfectly. All the best.

Sign in to comment.

More Answers (1)

Hi UH,
I understand that you have two sets of xy-data representing lines and want to create a contour plot with specified z values. The area of interest for the plot is defined as 0 to 120 in the x-axis and 0 to 220 in the y-axis.
Based on the provided information, I see that you have attempted to use meshgrid for plotting contour plots but are facing challenges converting the known values of lines into contours.
I recommend using "xlim" and "ylim" to plot the required area. The link to documentation is attached below
I have modified your code to help you with the same
xy = readtable('xy_data.xlsx');
x1 = xy.x40;
y1 = xy.y40;
x2 = xy.x30;
y2 = xy.y30;
figure
%% Commenting the rectangle function, use limits instead
% rectangle('Position',[0 0 120 220]) % the contour line can be only drawn in this box
axis equal
hold on
plot(x1,y1)
plot(x2,y2)
%% adding limits
xlim([0 120]);
ylim([0 220]);
Link to documentation:
  1. Set or query x-axis limits - MATLAB xlim - MathWorks India
  2. Specify Axis Limits - MATLAB & Simulink - MathWorks India
Hope this helps.
Regards,
Nipun

Categories

Products

Release

R2023b

Asked:

UH
on 11 Dec 2023

Answered:

on 21 Dec 2023

Community Treasure Hunt

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

Start Hunting!