1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
import os import matplotlib.pyplot as plt import numpy as np from scipy import interpolate os.chdir(r'D:\data\scripts\web1\ex28') os.getcwd() x = y = np.linspace(-2, 2, 20) def f(x, y): return np.exp(-(x + .5)**2 - 2*(y + .5)**2) - np.exp(-(x - .5)**2 - 2*(y - .5)**2) X, Y = np.meshgrid(x, y) # simulate noisy data at fixed grid points X, Y Z = f(X, Y) + 0.05 * np.random.randn(*X.shape) f_interp = interpolate.interp2d(x, y, Z, kind='cubic') xx = yy = np.linspace(x.min(), x.max(), 1000) ZZi = f_interp(xx, yy) XX, YY = np.meshgrid(xx, yy) fig, axes = plt.subplots(1, 2, figsize=(12, 5)) c = axes[0].contourf(XX, YY, f(XX, YY), 15, cmap=plt.cm.bwr) axes[0].set_xlabel(r"$x$", fontsize=20) axes[0].set_ylabel(r"$y$", fontsize=20) axes[0].set_title("exact / high sampling") cb = fig.colorbar(c, ax=axes[0]) cb.set_label(r"$z$", fontsize=20) c = axes[1].contourf(XX, YY, ZZi, 15, cmap=plt.cm.bwr) axes[1].set_ylim(-2.1, 2.1) axes[1].set_xlim(-2.1, 2.1) axes[1].set_xlabel(r"$x$", fontsize=20) axes[1].set_ylabel(r"$y$", fontsize=20) axes[1].scatter(X, Y, marker='.', color='k') axes[1].set_title("interpolation of noisy data / low sampling") cb = fig.colorbar(c, ax=axes[1]) cb.set_label(r"$z$", fontsize=20) fig.tight_layout() plt.savefig("example28.png", dpi=100) plt.show() plt.close() <img class=" wp-image-137 aligncenter" src="https://gantovnik.com/bio-tips/wp-content/uploads/2019/01/example28.png" alt="example28" width="604" height="251" /> |
Recent Comments