# Given a contour plot is there a function which returns a matrix corresponding to the density of lines at a given (x,y) coordinate?

10 views (last 30 days)
Marcus Rosales on 3 Apr 2024
Commented: Marcus Rosales on 4 Apr 2024
I simply have a contour plot and I would like to know if there is a function I can use to count the number of lines in a given region? I want a matrix whose intensity cooresponds to the number of lines in the vicinity of that point

Adam Danz on 3 Apr 2024
No, there is not a function that computes the density of contour lines. However, you could build such a function.
First, you have to get the contour line coordinates. There are several functions available on the MATLAB File Exchange that returns these values (e.g. getContourLineCoordinates).
Second, it would be wise to interpolate those coordinates so that each line has a fixed interval of points and all lines share the same interval. This way intervals won't bias the density measurments.
Third, you can use histcounts2 to count the number of coordinates within 2D bins. Those data can be plotted using histogram2, imagesc, pcolor, binscatter, and other density plots.
Note that this doesn't tell you the number of lines in a region. If you wanted the number of lines you'd have to take a few extra steps to identify what line each point comes from. The getContourLineCoordinates returns a table that identifies the line groups for each coordinate.
But before you start any of this, ask yourself whether analyzing contour lines is the best approach. Densely packed lines means steep slopes so you may want to analyze the 2d gradient of the data instead.
##### 3 CommentsShow 1 older commentHide 1 older comment
Adam Danz on 4 Apr 2024
Looks like John and I answered at the same time. His answer walks you through the advice I give in my last 2 sentences.
Marcus Rosales on 4 Apr 2024
Trust me I get this all very well... I am not a matlab expert, but I probably know more math than most people on here... His answer was a bit patronizing in parts, so I choose yours.
I am seeing if I can get better looking plots compiled from a different method because I have data not generated from a cos or sin... like an ideal world John is assuming I belong to.
There are bumps and wiggles which make the gradient not well behaved... Tried some filters first to help, but part of the issue has to do with putting in periodic boundary conditions on a lattice when an external guage field is present... The contour plots corresponding to my data look ok, so I was thinking maybe I can simply count the lines here, and get code which automatically returns the relevant scattering vectors needed to describe "quasi-particle-interference". I am sure contour does something similar to taking a grad, but I am just trying things.

John D'Errico on 3 Apr 2024
Edited: John D'Errico on 3 Apr 2024
Is there some specific function? No. But what does a high density of contour lines mean? It says the gradient is large in that vicinity. For example, I have no idea what this surface looks like in advance. Just some waves, some bumps.
fun = @(X,Y) sin(X+Y).*cos(X-2*Y);
[x,y] = meshgrid(linspace(0,pi));
z = fun(x,y);
surf(x,y,z)
contour(x,y,z,50)
axis equal
We can clearly see where the contour lines pile up in the plot. But we can also get a measure of that from this plot:
% The 2-norm of a vector is the sqrt of the sum of squares of the elements.
pcolor(x,y,sqrt(gx.^2 + gy.^2))
axis equal
colorbar
You can see the flat spots, generally the tops or bottoms of the bumps are represented in blue. Of course, we can also have a local flat spot at a saddle point. The regions of high contour density are yellow. Regions of intermediate contour density are seen as green in the pcolor plot.
Do you see that what I am computing here is the local norm of the gradient vector? Where that norm is a large number, then the function is changing rapidly. Where the norm of the gradient is small, we are in a locally flat region, and so there are few contour lines piling up.
Try it on another function. This time I'll use peaks. While I know the basic shape of the peaks function, I don't know where to expect the regions of maximum contour density will be in advance. I can make a guess though.
[x,y] = meshgrid(linspace(-3,3));
z = peaks(x,y);
surf(x,y,z)
contour(x,y,z,50) % lots of contours, so we can see where they pile up
axis equal
pcolor(x,y,sqrt(gx.^2 + gy.^2))
axis equal
colorbar
So the steepest region, with the highest contour density is the bright yellow blob near the bottom middle, near the point (x,y) = (0,-1). There is another region of high contour density near (0,1). And of course on the perimeter of the plot, the function is almost flat, so there are very few contour lines seen at all. That entire region will be dark blue.
If the function is known, so we have a functional form for it, then we could use an optimizer to search for the point where the norm of the gradient is maximized. On the peaks function, there would clearly be at least 3 local maxima of the norm f the gradient. Actually, I'd guess we could find at least 5 local maxima. (Not difficult to do, but please don't make me write code to find all of the local maxima of the norm of the gradient function. I'm just too lazy.)
Anyway, you should get the general idea from these examples. Why does the norm of the grient work for this? If a contour line represents the locus of points where the function takes on a specific fixed value, then if you have many contour lines in one region, it means the function is changing rapidly in that vicinity. Therefore the gradient would have a large norm. And while there is no explicit function that computes the contour density directly, it is almost trivial to do the computation.
##### 3 CommentsShow 1 older commentHide 1 older comment
John D'Errico on 4 Apr 2024
What about them did not anwer your question in a very direct way? I'm confused.
Marcus Rosales on 4 Apr 2024
... Ok? Here we go. This is my response to the accepted answer. I basically said thanks to you for the effort, but no I am no confused over the gradient... not sure how you arrive at the conclusion I must be confused still when I thanked you, but ok...
--------- My other response-----------
Trust me I get this all very well... I am not a matlab expert, but I probably know more math than most people on here... His answer was a bit patronizing in parts, so I choose yours.
I am seeing if I can get better looking plots compiled from a different method because I have data not generated from a cos or sin... like an ideal world John is assuming I belong to.
There are bumps and wiggles which make the gradient not well behaved... Tried some filters first to help, but part of the issue has to do with putting in periodic boundary conditions on a lattice when an external guage field is present... The contour plots corresponding to my data look ok, so I was thinking maybe I can simply count the lines here, and get code which automatically returns the relevant scattering vectors needed to describe the "quasi-particle-interference". I am sure contour does something similar to taking a grad, but I am just trying things.