Calling Contour Without Plotting It, Python, Pylab Inline
Solution 1:
The following is a modified code i used to get the points on a unit circle within in a declared meshgrid. It gives the contour points faster than plt.contour and doesn't plot the points.
The matplotlib._cntr is the core function called by plt.contour which tries to get the contour points.
import matplotlib._cntr as cntr
import numpy as np
# Test data.
x = np.linspace(-1, 1, 20)
y = np.linspace(-1, 1, 20)
x, y = np.meshgrid(x, y)
z = x**2 + y**2 - 1 # Function to get points from# the above function can be replaced with any curve equation# conics like ellipse or hyperbola: ((x**2)/a)+((y**2)/b)-1,etc.
level = 0
c = cntr.Cntr(x, y, z)
nlist = c.trace(level, level, 0)
segs = nlist[:len(nlist)//2]
print segs[0][0] # x,y coords of contour points.
Sorry for the poor explaination, I am not experienced enough with python. For a detailed explanation so you can refer to the link below.
link to discussion:http://matplotlib.1069221.n5.nabble.com/pyplot-Extract-contourset-without-plotting-td15868.html
At the end of discussion Mr.Ian Thomas has attached a code 'contour_test.py' which may be of help to you.
link to sample code:http://matplotlib.1069221.n5.nabble.com/attachment/15872/0/contour_test.py
Solution 2:
There is not specific option to suppress plotting of a contour (as far as I can see). The following question appears to provide exactly what you want using matplotlib._cntr
.
For your case, it may be simpler to achieve the suppression of a figure in pylab inline by switching back to a different gui, e.g. using %pylab qt
and then call cs = contour(image_matrix)
. This may not show anything without an explicit call to plt.show()
and you can use cs
to get the contour information you need.
You may also be able to use something like matplotlib.interactive(False)
to suppress the figure.
Solution 3:
Because matplotlib._cntr
is not supported anymore, you can use the find_contour()
function from skimage
. Here is a simple code to extract a contour level 0.8 from an analytical function from the documentation.
import numpy as np
from skimage import measure
# Construct some test data
x, y = np.ogrid[-np.pi:np.pi:100j, -np.pi:np.pi:100j]
r = np.sin(np.exp((np.sin(x)**3 + np.cos(y)**2)))
# Find contours at a constant value of 0.8
contours = measure.find_contours(r, 0.8)
This will give you the contour in function of (row, column) coordinates along the contour, and not the value of x
and y
. To convert to x
and y
values, you can then interpolate using interp1d
from scipy
:
from scipy.interpolate importinterp1dfx= interp1d(np.arange(0,x.shape[0]), x.flatten())
fy = interp1d(np.arange(0,y.shape[1]), y.flatten())
for contour in contours:
contour[:,0] = fx(contour[:,0])
contour[:,1] = fy(contour[:,1])
Simple code to see the results and validate:
import matplotlib.pyplot as pltfig= plt.figure()
ax = fig.add_subplot(111)
for contour in contours:
ax.plot(contour[:,0], contour[:,1])
fig.show()
Post a Comment for "Calling Contour Without Plotting It, Python, Pylab Inline"