import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure(figsize=(9, 6))
# 定义等高线高度函数
def f(x, y):
return (pow(x,3)+pow(y,3)-15*x-20*y)+9
# 数据数目
n = 256
# 定义x, y
x = np.linspace(-5, 5, n)
y = np.linspace(-3, 2, n)
# 生成网格数据
X, Y = np.meshgrid(x, y)
# 填充等高线的颜色, 8是等高线分为几部分
plt.contourf(X, Y, f(X, Y), 8, alpha = 0.75, cmap = plt.cm.prism)
# 绘制等高线
C = plt.contour(X, Y, f(X, Y), 8, colors = 'black', linewidth = 0.5)
#使得上面右边都有刻度线,direction指明刻度线方向,可选out
plt.tick_params(right=True, top=True,labelsize=10, direction='in', pad=3, length=4)
x_ticks = np.linspace(5, -5, 8)#发现如果是np.arrange(5,-5,8)就不能画上去
x_labels = ['1990', '1991', '1992', '1993', '1994', '1995','1996','1997']
y_ticks = np.linspace(-3, 2, 5)
y_labels = [''