天天看點

Python 之 matplotlib (九)contours等高線

代碼:

import matplotlib.pyplot as plt
import numpy as np


def f(x, y):
    return (1 - x / 2 + X ** 3) * np.exp(-x ** 2 - y ** 2)


n = 256
x = np.linspace(-3, 3, n)
y = np.linspace(-3, 3, n)
X, Y = np.meshgrid(x, y)  # 網格輸入值

# use plt.contour to filling contours
# X,Yand value for (X,Y)point
plt.contourf(X, Y, f(X, Y), 8,
             alpha=0.75, cmap=plt.cm.hot)

# use plt.contour to add contourlines
C = plt.contour(X, Y, f(X, Y), 9, color='balck', linewidth=.5)

# adding label
plt.clabel(C, inlines=True)

plt.xticks(())
plt.yticks(())
plt.show()
           

運作結果:

Python 之 matplotlib (九)contours等高線