注意
前往結尾下載完整的範例程式碼。
填充 3D 線圖下方#
示範如何建立填充線圖下方空間的多邊形。在此範例中,多邊形是半透明的,產生一種「鋸齒狀彩色玻璃」效果。
import math
import matplotlib.pyplot as plt
import numpy as np
gamma = np.vectorize(math.gamma)
N = 31
x = np.linspace(0., 10., N)
lambdas = range(1, 9)
ax = plt.figure().add_subplot(projection='3d')
facecolors = plt.colormaps['viridis_r'](np.linspace(0, 1, len(lambdas)))
for i, l in enumerate(lambdas):
# Note fill_between can take coordinates as length N vectors, or scalars
ax.fill_between(x, l, l**x * np.exp(-l) / gamma(x + 1),
x, l, 0,
facecolors=facecolors[i], alpha=.7)
ax.set(xlim=(0, 10), ylim=(1, 9), zlim=(0, 0.35),
xlabel='x', ylabel=r'$\lambda$', zlabel='probability')
plt.show()

指令碼總執行時間: (0 分鐘 1.121 秒)