3D 曲面(颜色映射)#

演示了如何绘制使用 coolwarm 颜色映射着色的 3D 曲面。通过使用 antialiased=False 使曲面不透明。

还演示了如何使用 LinearLocator 以及为 z 轴刻度标签进行自定义格式化。

import matplotlib.pyplot as plt
import numpy as np

from matplotlib import cm
from matplotlib.ticker import LinearLocator

fig, ax = plt.subplots(subplot_kw={"projection": "3d"})

# Make data.
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)

# Plot the surface.
surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,
                       linewidth=0, antialiased=False)

# Customize the z axis.
ax.set_zlim(-1.01, 1.01)
ax.zaxis.set_major_locator(LinearLocator(10))
# A StrMethodFormatter is used automatically
ax.zaxis.set_major_formatter('{x:.02f}')

# Add a color bar which maps values to colors.
fig.colorbar(surf, shrink=0.5, aspect=5)

plt.show()
surface3d

标签:绘图类型:3D 样式:颜色映射 级别:高级

脚本总运行时间: (0 分 1.507 秒)

由 Sphinx-Gallery 生成的画廊