1 绘制等高线图
import numpy as np
import matplotlib.pyplot as plt
#计算高度
def calcu_elevation(x1, y1):
h = (1-x1/2 + x1 ** 5 + y1 ** 3) * np.exp(-x1** 2 - y1** 2)
return h
n = 256
x = np.linspace(-2, 2, n)
y = np.linspace(-2, 2, n)
#利用 meshgrid() 函数生成网格数据
x_grid, y_grid = np.meshgrid(x, y)
fig = plt.figure()
ax = fig.add_subplot(111)
#绘制等高线
con = ax.contour(x_grid, y_grid, calcu_elevation(x_grid, y_grid), 8, colors=‘black’)
#填充等高线的颜色
ax.contourf(x_grid, y_grid, calcu_elevation(x_grid, y_grid), 8, alpha=0.75, cmap=plt.cm.copper)
#为等高线添加文字标签
ax.clabel(con, inline=True, fmt=‘%1.1f’, fontsize=10)
ax.set_xticks([])
ax.set_yticks([])
plt.show()
2 绘制矢量场流线图
import numpy as np
import matplotlib.pyplot as plt
y, x = np.mgrid[0:5:50j, 0:5:50j]
u = x
v = y
fig = plt.figure()
ax = fig.add_subplot(111)
#绘制矢量场流线图
ax.streamplot(x, y, u, v)
plt.show()
3 绘制棉棒图
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams[‘font.sans-serif’] = ‘SimHei’
plt.rcParams[‘axes.unicode_minus’] = False
x = np.arange(1, 16)
y = np.array([5.9, 6.2, 6.7, 7.0, 7.0, 7.1, 7.2, 7.4,
7.5, 7.6, 7.7, 7.7, 7.7, 7.8, 7.9])
labels = np.array([‘宝骏310’, ‘宝马i3’, ‘致享’, ‘焕驰’, ‘力帆530’,
‘派力奥’, ‘悦翔V3’, ‘乐风RV’, ‘奥迪A1’, ‘威驰FS’,
‘夏利N7’, ‘启辰R30’, ‘和悦A13RS’, ‘致炫’, ‘赛欧’])
fig = plt.figure(figsize=(10, 6), dpi= 80)
ax = fig.add_subplot(111)
#绘制棉棒图
markerline, stemlines, baseline = ax.stem(x, y, linefmt=‘–’,
markerfmt=‘o’, label=‘TestStem’, use_line_collection=True)
#设置棉棒图线段的属性
plt.setp(ste