plt.gca().set_aspect('equal')
x, y = np.meshgrid(np.linspace(-1, 1, 100), np.linspace(-1, 1, 100))
r = x * x + y * y
plt.contour(np.log(r / 1.2))
ca = plt.gca()
N = len(ca.collections)
for n in range(N):
    c = ca.collections.pop(0)
for s in c.get_segments():
    closed = abs(s[0, 0] - s[-1, 0]) < eps and abs(s[0, 1] - s[-1, 1]) < eps
p = PolyCollection([s], edgecolors=c.get_edgecolors(), linewidths=c.
    get_linewidths(), linestyles=c.get_linestyles(), facecolors=c.
    get_facecolors(), closed=closed)
ca.add_collection(p)
plt.savefig('test.pdf')
Closed lines in matplotlib contour plots