Draw Lines with Uniform Distance Function in MATLAB

I would like to draw the function height lines (represented by matrices of course) using MATLAB.

I am familiar with the path, but the path draws lines at heights at an even distance, while I would like to see lines (with height marks) at a constant distance apart when plotting.

This means that if the function grows quickly in one area, I will not get a graph with dense height lines, but just a few lines at evenly spaced distances.

I tried looking for such an option on the outline help page but didn't see anything. Is there a built-in function that does this?

+3


source to share


2 answers


There is no built-in function for this (as far as I know). You should understand that in general you cannot have lines that both represent iso-values ​​and that are located at a fixed distance. This is only possible with charts that have special scaling properties, and again, this is not a common case.

Having said that, you can imagine to approach your desired plot using the syntax in which you specify the levels for the graphs:

...
contour(Z,v) draws a contour plot of matrix Z with contour lines at the data values specified in the monotonically increasing vector v.
...

      

So all you need is a good vector v

of height values. For this we can take a classic Matlab example:

[X,Y,Z] = peaks;
contour(X,Y,Z,10);
axis equal
colorbar

      

enter image description here



and convert it to:

[X,Y,Z] = peaks;
[~, I] = sort(Z(:));
v = Z(I(round(linspace(1, numel(Z),10))));
contour(X,Y,Z,v);
axis equal
colorbar

      

enter image description here

The result may not be as pleasant as you expected, but this is the best I can think of, given that what you are asking is again not possible.

Best,

+2


source


One thing you could do is not construct the paths at equal levels of space (this is what happens when you pass an integer to a path), to construct the paths at a fixed percentile of your data (this requires you to pass a vector of levels to contour):

Z = peaks(100);  % generate some pretty data
nlevel = 30;

subplot(121)
contour(Z, nlevel)  % spaced equally between min(Z(:)) and max(Z(:))
title('Contours at fixed height')

subplot(122)
levels = prctile(Z(:), linspace(0, 100, nlevel));
contour(Z, levels);  % at given levels
title('Contours at fixed percentiles')

      



Result: enter image description here

For a correct figure, the lines are somewhat equal for most of the image. Note that the distance is approximately equal, and it is impossible to get an equal distance across the entire image, except in some trivial cases.

+1


source







All Articles