Given a contour plot is there a function which returns a matrix corresponding to the density of lines at a given (x,y) coordinate?
8 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Marcus Rosales
el 3 de Abr. de 2024
Comentada: Marcus Rosales
el 4 de Abr. de 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
0 comentarios
Respuesta aceptada
Adam Danz
el 3 de Abr. de 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 comentarios
Adam Danz
el 4 de Abr. de 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.
Más respuestas (1)
John D'Errico
el 3 de Abr. de 2024
Editada: John D'Errico
el 3 de Abr. de 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:
[gx,gy] = gradient(z);
% 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))
shading interp
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
[gx,gy] = gradient(z);
pcolor(x,y,sqrt(gx.^2 + gy.^2))
shading interp
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 comentarios
John D'Errico
el 4 de Abr. de 2024
What about them did not anwer your question in a very direct way? I'm confused.
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!