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

3 views (last 30 days)

Show older comments

Chris Nemecek on 31 May 2024

  • Link

    Direct link to this question

    https://ms-www.mathworks.com/matlabcentral/answers/2124486-contour-plot-from-x-and-y-data-points-with-corresponding-contour-level

  • Link

    Direct link to this question

    https://ms-www.mathworks.com/matlabcentral/answers/2124486-contour-plot-from-x-and-y-data-points-with-corresponding-contour-level

Commented: William Rose on 2 Jun 2024

  • example.mat

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

Show -1 older commentsHide -1 older comments

John D'Errico on 31 May 2024

Direct link to this comment

https://ms-www.mathworks.com/matlabcentral/answers/2124486-contour-plot-from-x-and-y-data-points-with-corresponding-contour-level#comment_3177161

  • Link

    Direct link to this comment

    https://ms-www.mathworks.com/matlabcentral/answers/2124486-contour-plot-from-x-and-y-data-points-with-corresponding-contour-level#comment_3177161

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.

Sign in to answer this question.

Answers (1)

  • Link

    Direct link to this answer

    https://ms-www.mathworks.com/matlabcentral/answers/2124486-contour-plot-from-x-and-y-data-points-with-corresponding-contour-level#answer_1466121

  • Link

    Direct link to this answer

    https://ms-www.mathworks.com/matlabcentral/answers/2124486-contour-plot-from-x-and-y-data-points-with-corresponding-contour-level#answer_1466121

Open in MATLAB Online

  • example.mat

@Chris Nemecek.

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])

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

4 Comments

Show 2 older commentsHide 2 older comments

William Rose on 1 Jun 2024

Direct link to this comment

https://ms-www.mathworks.com/matlabcentral/answers/2124486-contour-plot-from-x-and-y-data-points-with-corresponding-contour-level#comment_3177186

  • Link

    Direct link to this comment

    https://ms-www.mathworks.com/matlabcentral/answers/2124486-contour-plot-from-x-and-y-data-points-with-corresponding-contour-level#comment_3177186

Edited: William Rose on 1 Jun 2024

Open in MATLAB Online

  • example.mat

@Chris Nemecek,

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

Chris Nemecek on 1 Jun 2024

Direct link to this comment

https://ms-www.mathworks.com/matlabcentral/answers/2124486-contour-plot-from-x-and-y-data-points-with-corresponding-contour-level#comment_3177356

  • Link

    Direct link to this comment

    https://ms-www.mathworks.com/matlabcentral/answers/2124486-contour-plot-from-x-and-y-data-points-with-corresponding-contour-level#comment_3177356

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.

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

William Rose on 2 Jun 2024

Direct link to this comment

https://ms-www.mathworks.com/matlabcentral/answers/2124486-contour-plot-from-x-and-y-data-points-with-corresponding-contour-level#comment_3177541

  • Link

    Direct link to this comment

    https://ms-www.mathworks.com/matlabcentral/answers/2124486-contour-plot-from-x-and-y-data-points-with-corresponding-contour-level#comment_3177541

@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.

William Rose on 2 Jun 2024

Direct link to this comment

https://ms-www.mathworks.com/matlabcentral/answers/2124486-contour-plot-from-x-and-y-data-points-with-corresponding-contour-level#comment_3177546

  • Link

    Direct link to this comment

    https://ms-www.mathworks.com/matlabcentral/answers/2124486-contour-plot-from-x-and-y-data-points-with-corresponding-contour-level#comment_3177546

@Chris Nemecek, this data reminds me of a convection-diffusion problem.

Sign in to comment.

Sign in to answer this question.

See Also

Tags

  • contour
  • contour plot

Products

  • MATLAB

Release

R2023b

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


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

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

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

FAQs

How to plot a contour plot? ›

You create a contour diagram corresponding to a function z = f(x, y) by creating a topographical map of its graph. You choose equally spaced elevations z = c for a bunch of values c, you find 1 Page 2 points on the graph for each elevation z = c, and then you project the curves on the graph onto the xy-plane.

How are contour plots calculated? ›

Each contour is obtained by slicing the surface with the horizontal plane z = c, so the equation for the contour at height c is simply f(x, y) = c. (b) f(x, y) = cospx2 + y2. The two images below show the graph of f(x, y) = x2 + y2 being sliced by horizontal planes, and the accompanying contour diagram.

How to plot contour plot in Minitab? ›

To display a contour plot for a mixture design, choose Stat > DOE > Mixture > Contour/Surface Plots. Select Contour plot and then click the corresponding Setup button. From Response, select the response variable that you want to plot.

How to plot contours of a function in Matlab? ›

fcontour( f ) plots the contour lines of the function z = f(x,y) for constant levels of z over the default interval [-5 5] for x and y . fcontour( f , xyinterval ) plots over the specified interval. To use the same interval for both x and y , specify xyinterval as a two-element vector of the form [min max] .

What is levels in contour plot? ›

A contour of the function z(x,y) is a set of points in the (x,y) plane, such that z(x,y) is fixed at some constant value. That constant value is the contour "level". That set of points in the (x,y) plane is often called a level set.

How to find the equation of a contour? ›

A contour is obtained by slicing the surface with a horizontal plane with equation z = c. Thus, the equation for the contour at height c is given by: f(x, y) = c. contour diagram to the graph of f.

What does a contour plot tell you? ›

A contour plot provides a two-dimensional view in which all points that have the same response are connected to produce contour lines of constant responses. Contour plots are useful for investigating desirable response values and operating conditions.

What is a contour plot from a function? ›

ContourPlot is also known as an isoline, isocurve, level set or sublevel set. When given a function f, ContourPlot constructs contour curves corresponding to the level sets where f[x,y] has constant values d1, d2, etc.

What is a contour plot surface plot? ›

Contour plots are useful for establishing the response values and operating conditions that you want. A 3D surface plot displays a 3-dimensional view of the surface. Like contour plots, 3D surface plots are useful for establishing response values and operating conditions that you want.

How do you show contour levels in MATLAB? ›

Specify levels as a scalar value n to display the contour lines at n automatically chosen levels (heights). To draw the contour lines at specific heights, specify levels as a vector of monotonically increasing values. To draw the contours at one height ( k ), specify levels as a two-element row vector [k k] .

What is contour plot in MATLAB? ›

A contour plot represents a 3-D surface by plotting lines that connect points with common z-values along a slice. For example, you can use a contour plot to visualize the height of a surface in two or three dimensions.

How to plot contour surface in MATLAB? ›

surfc( Z ) creates a surface and contour plot and uses the column and row indices of the elements in Z as the x- and y -coordinates. surfc( Z , C ) additionally specifies the surface color. surfc( ax ,___) plots into the axes specified by ax instead of the current axes.

How do you draw a contour structure? ›

Structure contours of a plane follow these rules:

the contour lines are evenly spaced, with spacing distance that is a function of the dip. from strike and dip one can construct the structure contours if you know the x,y,z position of one point, or can calculate strike and dip if giving the structure contours.

What is the basic rule of contour line drawing? ›

Rules of Contour Lines

By definition, contour lines separate points of higher elevation from points of lower elevation. This means it is always possible to define an “uphill” direction on one side of a contour and a “downhill” direction on the other side. Contour lines never cross.

References

Top Articles
Latest Posts
Article information

Author: Chrissy Homenick

Last Updated:

Views: 6250

Rating: 4.3 / 5 (54 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Chrissy Homenick

Birthday: 2001-10-22

Address: 611 Kuhn Oval, Feltonbury, NY 02783-3818

Phone: +96619177651654

Job: Mining Representative

Hobby: amateur radio, Sculling, Knife making, Gardening, Watching movies, Gunsmithing, Video gaming

Introduction: My name is Chrissy Homenick, I am a tender, funny, determined, tender, glorious, fancy, enthusiastic person who loves writing and wants to share my knowledge and understanding with you.