Từ liên kết bạn đã đề xuất, ví dụ ở đây có lẽ là gần nhất với những gì bạn muốn. Bạn có thể sử dụng ví dụ với các giá trị của mình,
import numpy as np
from scipy import interpolate
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d, Axes3D
X, Y = np.mgrid[-1:1:20j, -1:1:20j]
Z = [X+Y] * np.exp[-6.0*[X*X+Y*Y]] + np.random.rand[X.shape[0]]
xnew, ynew = np.mgrid[-1:1:80j, -1:1:80j]
tck = interpolate.bisplrep[X, Y, Z, s=0]
znew = interpolate.bisplev[xnew[:,0], ynew[0,:], tck]
fig = plt.figure[figsize=[12,12]]
ax = fig.gca[projection='3d']
ax.plot_surface[X, Y, Z, cmap='summer', rstride=1, cstride=1, alpha=None]
plt.show[]
fig = plt.figure[figsize=[12,12]]
ax = fig.gca[projection='3d']
ax.plot_surface[xnew, ynew, znew, cmap='summer', rstride=1, cstride=1, alpha=None, antialiased=True]
plt.show[]
Ngoài ra, antialiased=True
có thể làm cho nó trông đẹp hơn nhưng tôi nghĩ là theo mặc định. Cốt truyện đầu tiên trông như thế này,
Và cốt truyện được làm mịn như thế này,
Vấn đề với nhiễu tần số thấp trong dữ liệu của bạn là rất khó để xác định một lưới đủ tốt để giải quyết. Bạn có thể điều chỉnh mức độ làm mịn với đối số s
thành interpolate.bisplrep
hoặc có thể là hạt thô/lọc dữ liệu của bạn chỉ để lại các xu hướng chính [ví dụ: sử dụng scipy.ndimage.interpolation.zoom
nếu bạn có dữ liệu thông thường]. Ngoài ra, hãy xem xét một loại lô khác như pcolormesh vì dữ liệu về cơ bản là 2D.
Ghi chú
Nhấn vào đây để tải xuống mã ví dụ đầy đủhere to download the full example code
Thể hiện việc sử dụng Hillshading tùy chỉnh trong cốt truyện bề mặt 3D.
# This import registers the 3D projection, but is otherwise unused. from mpl_toolkits.mplot3d import Axes3D # noqa: F401 unused import from matplotlib import cbook from matplotlib import cm from matplotlib.colors import LightSource import matplotlib.pyplot as plt import numpy as np # Load and format data with cbook.get_sample_data['jacksboro_fault_dem.npz'] as file, \ np.load[file] as dem: z = dem['elevation'] nrows, ncols = z.shape x = np.linspace[dem['xmin'], dem['xmax'], ncols] y = np.linspace[dem['ymin'], dem['ymax'], nrows] x, y = np.meshgrid[x, y] region = np.s_[5:50, 5:50] x, y, z = x[region], y[region], z[region] # Set up plot fig, ax = plt.subplots[subplot_kw=dict[projection='3d']] ls = LightSource[270, 45] # To use a custom hillshading mode, override the built-in shading and pass # in the rgb colors of the shaded surface calculated from "shade". rgb = ls.shade[z, cmap=cm.gist_earth, vert_exag=0.1, blend_mode='soft'] surf = ax.plot_surface[x, y, z, rstride=1, cstride=1, facecolors=rgb, linewidth=0, antialiased=False, shade=False] plt.show[]
Từ khóa: Ví dụ về mã Matplotlib, Codex, Python Plot, Pyplot Gallery được tạo bởi Sphinx-Gallery