I am trying to compute the contour lines of a numpy NDarray.
However, I do not want to plot them but simply to do some analysis with the obtained lines.
So far I managed to get a very similar result by creating a figure and then closing it right after I computed the contour lines:
x = np.linspace(-3.0, 3.0, 100)
y = np.linspace(-3.0, 3.0, 100)
Z = np.sin(X) * np.cos(Y)
fig, ax = plt.subplots()
cont_obj = ax.contour(x, y, Z)
contours = []
for seg in contour_obj.allsegs:
for val in seg:
if len(val) > 0:
xc = val[:,0]
yc = val[:,1]
contours.append([xc,yc])
plt.close(fig)
This solution seems to work. However, I would love to overcome one issue, which is the creation of the external subplot.
I know that matplotlib._cntr is not available anymore, which makes everything more complicated. I know that alternatives, such as matplotlib/legacycontour, exist. However, is unclear to me if this module is embedded in the standard matplotlib package (would be great) or not (I would avoid it).
1