Get coordinates from contour in matplotlib?

Background

An example of documentation is given from here ; you can easily create the following code fragment with a code fragment.

import matplotlib import numpy as np import matplotlib.cm as cm import matplotlib.mlab as mlab import matplotlib.pyplot as plt matplotlib.rcParams['xtick.direction'] = 'out' matplotlib.rcParams['ytick.direction'] = 'out' delta = 0.025 x = np.arange(-3.0, 3.0, delta) y = np.arange(-2.0, 2.0, delta) X, Y = np.meshgrid(x, y) Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0) Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1) # difference of Gaussians Z = 10.0 * (Z2 - Z1) # Create a simple contour plot with labels using default colors. The # inline argument to clabel will control whether the labels are draw # over the line segments of the contour, removing the lines beneath # the label plt.figure() CS = plt.contour(X, Y, Z) plt.clabel(CS, inline=1, fontsize=10) plt.title('Simplest default with labels') 

enter image description here

My goal

I got my outline graph and meanwhile I got an instance of matplotlib.contour.QuadContourSet CS . In the sample snippet, CS used only for clabel() . However, for my case, I need to get either the contour line equation or a set of coordinates for further calculation.

How to extract contour line coordinates from a CS instance? OR How can I achieve this in other ways?

I bet there should be a way to do this. Otherwise, the contour thing is just a "vase for visualization."

+6
source share
1 answer

You can get the coordinates of the contours from the CS.allsegs list.

Try:

 dat0= CS.allsegs[0][0] plt.plot(dat0[:,0],dat0[:,1]) 

to build the first (-1) contour level.

+11
source

Source: https://habr.com/ru/post/956137/


All Articles