calling contour without plotting it, python, pylab inline

冷暖自知 提交于 2019-12-13 13:22:42

问题


For an algorithm I am using contour, but I'm only interested in its collection of paths. Since I have called

pylab inline

from the start, and it is now too painful to rewrite the code without the inline (many functions have to be declared more carefully, like np.something() instead of something(), etc...), I was wondering if there is a way to call contour without it plotting the contour map ? Something like

contour(image_matrix, 'No Show')? 

Regards


回答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




回答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.




回答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 import interp1d
fx = 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 plt
fig = plt.figure()
ax = fig.add_subplot(111)
for contour in contours:
    ax.plot(contour[:,0], contour[:,1])
fig.show()

Figure of the extracted contours



来源:https://stackoverflow.com/questions/30376897/calling-contour-without-plotting-it-python-pylab-inline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!