Hướng dẫn python contour irregular grid - lưới không đều đường viền python
Tôi có một tập dữ liệu với ba cột, vị trí x và y, sau đó chiều cao/màu z. Theo lời khuyên được đưa ra ở đây làm cho đường viền phân tán, tôi đang cố gắng thực hiện một số lô đường viền. Tôi đã sử dụng mã này nhiều lần trong quá khứ, nhưng dường như nó không hoạt động cho bộ dữ liệu cụ thể này. Khi tôi vẽ dữ liệu như một biểu đồ phân tán màu, tôi nhận được điều này: Trông có vẻ chính xác, nhưng khi tôi cố gắng biến dữ liệu đó thành một biểu đồ đường viền, đầu ra là thế này: Nhưng toàn bộ cốt truyện nên được điền vào, đặc biệt là vì tôi đang sử dụng phép nội suy tuyến tính với GridData để điền vào các điểm bị thiếu. Tôi đang làm gì sai? Mã này hoạt động cho các bộ dữ liệu khác ... Đây là mã của tôi: Ghi chú Nhấn vào đây để tải xuống mã ví dụ đầy đủhere to download the full example code So sánh một biểu đồ đường viền của dữ liệu cách đều nhau không đều được nội suy trên lưới thông thường so với biểu đồ Tricontour cho một lưới hình tam giác không cấu trúc. Kể từ khi
Ví dụ này cho thấy cả hai phương thức trong hành động.
import matplotlib.pyplot as plt import matplotlib.tri as tri import numpy as np np.random.seed(19680801) npts = 200 ngridx = 100 ngridy = 200 x = np.random.uniform(-2, 2, npts) y = np.random.uniform(-2, 2, npts) z = x * np.exp(-x**2 - y**2) fig, (ax1, ax2) = plt.subplots(nrows=2) # ----------------------- # Interpolation on a grid # ----------------------- # A contour plot of irregularly spaced data coordinates # via interpolation on a grid. # Create grid values first. xi = np.linspace(-2.1, 2.1, ngridx) yi = np.linspace(-2.1, 2.1, ngridy) # Linearly interpolate the data (x, y) on a grid defined by (xi, yi). triang = tri.Triangulation(x, y) interpolator = tri.LinearTriInterpolator(triang, z) Xi, Yi = np.meshgrid(xi, yi) zi = interpolator(Xi, Yi) # Note that scipy.interpolate provides means to interpolate data on a grid # as well. The following would be an alternative to the four lines above: # from scipy.interpolate import griddata # zi = griddata((x, y), z, (xi[None, :], yi[:, None]), method='linear') ax1.contour(xi, yi, zi, levels=14, linewidths=0.5, colors='k') cntr1 = ax1.contourf(xi, yi, zi, levels=14, cmap="RdBu_r") fig.colorbar(cntr1, ax=ax1) ax1.plot(x, y, 'ko', ms=3) ax1.set(xlim=(-2, 2), ylim=(-2, 2)) ax1.set_title('grid and contour (%d points, %d grid points)' % (npts, ngridx * ngridy)) # ---------- # Tricontour # ---------- # Directly supply the unordered, irregularly spaced coordinates # to tricontour. ax2.tricontour(x, y, z, levels=14, linewidths=0.5, colors='k') cntr2 = ax2.tricontourf(x, y, z, levels=14, cmap="RdBu_r") fig.colorbar(cntr2, ax=ax2) ax2.plot(x, y, 'ko', ms=3) ax2.set(xlim=(-2, 2), ylim=(-2, 2)) ax2.set_title('tricontour (%d points)' % npts) plt.subplots_adjust(hspace=0.5) plt.show() Phòng trưng bày được tạo ra bởi Sphinx-Gallery |