Vertex sampling is designed for polygon simplification. There is a choice of two implementation algorithms: Douglas-Peucker and Visvalingam-Whyatt.
import numpy as np from lets_plot import * LetsPlot.setup_html()
def f(x, y, n): z = np.sin(x * 3 * np.pi / n) z += np.sin(y * 3 * np.pi / n) z += x * 3 / n z += y * 5 / n return z n = 50 x = np.arange(n) y = np.arange(n) X, Y = np.meshgrid(x, y) Z = f(X, Y, n) data = dict( x=X.reshape(-1), y=Y.reshape(-1), z=Z.reshape(-1))
p = ggplot(data, aes('x','y', z='z')) \ + theme_classic() + theme(axis='blank') + ggsize(200, 200) p + geom_contour()
# Douglas-Peucker algorithm in this case works better than others. # The lines become ragged but the shape remained unchanged. p + geom_contour(sampling=sampling_vertex_dp(200))
# Visvalingam-Whyatt algorithm is less accurate with rings: they became unclosed. p + geom_contour(sampling=sampling_vertex_vw(200))