Extract the x- and y-locations from the contour matrix C

Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Extract the x- and y-locations from the contour matrix C

matplotlib - users mailing list
Hi All,

in Octave you can do something like this

% Define the input grid
[x, y] = meshgrid(linspace(-2, 2));
% Calculate the two surfaces
%z1 = y.^2 + 2*x;
z1 = exp(-(x.^2 + y.^2));
%z2 = 2*y.^3 - x.^2;
z2 =  0.4+0.0*x+0.0*y;
% Visualize the two surfaces
figure(1);
surface(x, y, z1, 'FaceColor', [0.5 1.0 0.5], 'EdgeColor', 'none');
surface(x, y, z2, 'FaceColor', [1.0 0.5 0.0], 'EdgeColor', 'none');
view(3); camlight; axis vis3d
% Take the difference between the two surface heights and find the contour
% where that surface is zero.
zdiff = z1 - z2;
%C = contours(x, y, zdiff, [0 0]);
figure(2);
contour(x, y, zdiff, [0 0]);
C = contour(x, y, zdiff, [0 0]);
% Extract the x- and y-locations from the contour matrix C.
xL = C(1, 2:end)
yL = C(2, 2:end)
% Interpolate on the first surface to find z-locations for the intersection
% line.
zL = interp2(x, y, z1, xL, yL);
% Visualize the line.
figure(3);
line(xL, yL, zL, 'Color', 'k', 'LineWidth', 3);

How can we extract the x- and y-locations from the contour matrix C in matplotlib?



_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Extract the x- and y-locations from the contour matrix C

Slavin, Jonathan
Hi Nils,

Extracting the x and y locations from a contour is a bit of a pain in matplotlib (or at least was last time I tried it). if you do something like
c = ax.contour(x,y,z,...)
(where ... stands in for the arguments you might want to supply)
then c is a QuadContourSet. To get at the contour data you need to do something like
p = c.collections[i].get_paths()
(if you want the i'th collection, i.e. contour level number i, or you could iterate of c.collections)
then p is a list of Path objects corresponding to each separate contour.
p.vertices is then an N x 2 array of the x and y values

An alternative to this is to use scikit-image package, in particular skimage.measure.find_contours. However in that case you do something like:
c = find_contours(image,level)
(where image is a 2D array) and you get back a list of (n,2) arrays with the indices of the image for the contour. To get the x,y values then you need to scale those indices accordingly.

Jon
Date: Wed, 6 Nov 2019 12:41:55 +0100
From: Nils Wagner <[hidden email]>
To: [hidden email]
Subject: [Matplotlib-users] Extract the x- and y-locations from the
        contour matrix C
Message-ID:
        <CAE7vhgN0cKXf54hvgT4Y=up_vYFL-o=w=[hidden email]>
Content-Type: text/plain; charset="utf-8"

Hi All,

in Octave you can do something like this

% Define the input grid
[x, y] = meshgrid(linspace(-2, 2));
% Calculate the two surfaces
%z1 = y.^2 + 2*x;
z1 = exp(-(x.^2 + y.^2));
%z2 = 2*y.^3 - x.^2;
z2 =  0.4+0.0*x+0.0*y;
% Visualize the two surfaces
figure(1);
surface(x, y, z1, 'FaceColor', [0.5 1.0 0.5], 'EdgeColor', 'none');
surface(x, y, z2, 'FaceColor', [1.0 0.5 0.0], 'EdgeColor', 'none');
view(3); camlight; axis vis3d
% Take the difference between the two surface heights and find the contour
% where that surface is zero.
zdiff = z1 - z2;
%C = contours(x, y, zdiff, [0 0]);
figure(2);
contour(x, y, zdiff, [0 0]);
C = contour(x, y, zdiff, [0 0]);
% Extract the x- and y-locations from the contour matrix C.
xL = C(1, 2:end)
yL = C(2, 2:end)
% Interpolate on the first surface to find z-locations for the intersection
% line.
zL = interp2(x, y, z1, xL, yL);
% Visualize the line.
figure(3);
line(xL, yL, zL, 'Color', 'k', 'LineWidth', 3);

How can we extract the x- and y-locations from the contour matrix C in
matplotlib?
--
Jonathan D. Slavin
Astrophysicist - High Energy Astrophysics Division
Center for Astrophysics | Harvard & Smithsonian
Office: (617) 496-7981 | Cell: (781) 363-0035
60 Garden Street | MS 83 | Cambridge, MA 02138



_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users