

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 ylocations from the contour matrix C. xL = C(1, 2:end) yL = C(2, 2:end) % Interpolate on the first surface to find zlocations 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 ylocations from the contour matrix C in matplotlib?
_______________________________________________
Matplotlibusers mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlibusers


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 scikitimage 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: [Matplotlibusers] Extract the x and ylocations from the
contour matrix C
MessageID:
<CAE7vhgN0cKXf54hvgT4Y=up_vYFLo=w=[hidden email]>
ContentType: text/plain; charset="utf8"
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 ylocations from the contour matrix C.
xL = C(1, 2:end)
yL = C(2, 2:end)
% Interpolate on the first surface to find zlocations 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 ylocations from the contour matrix C in
matplotlib?
 Jonathan D. Slavin Astrophysicist  High Energy Astrophysics Division Center for Astrophysics  Harvard & Smithsonian Office: (617) 4967981  Cell: (781) 3630035 60 Garden Street  MS 83  Cambridge, MA 02138
_______________________________________________
Matplotlibusers mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlibusers

