python数据可视化5

3. 多图和多轴绘制

MATLAB和Pyplot具有当前图形(current figure)和当前轴(current axes)的概念。记住,一个图中可以有多个轴,每个图线都在一定轴范围内进行绘制。所有绘图命令都只适用于当前轴。函数gca()将返回当前轴(一个matplotlib.axes.Axes实例),gcf()将返回当前图(一个matplotlib.figure.Figure实例)。

import numpy as np
import matplotlib.pyplot as plt

def f(t):
    return np.exp(-t) * np.cos(2*np.pi*t)

t1 = np.arange(0.0, 5.0, 0.1)
t2 = np.arange(0.0, 5.0, 0.02)

plt.figure(1)
plt.subplot(211)
plt.plot(t1, f(t1), 'bo', t2, f(t2), 'k')

plt.subplot(212)
plt.plot(t2, np.cos(2*np.pi*t2), 'r--')
plt.figure(1)                # 第一个图
zitu1=plt.subplot(211)             # 第一个图中的第一个子图
plt.plot(t, t**2)
zitu2=plt.subplot(212)             # 第一个图中的第二个子图
plt.plot(t, np.cos(t))

plt.figure(2)                # 第二个图
plt.plot(t, np.exp(t))       # 默认创建subplot(111)

zitu1.set_title('the first subplot in the first figure')

如果画图太多,内存受限,记得调用close()命令释放内存。

plt.close('all')

知识来源于博雅数据

posted @ 2021-01-15 11:18  藻类植物  阅读(70)  评论(0编辑  收藏  举报