使用matplotlib,同时在多个figure画图
生活随笔
收集整理的这篇文章主要介绍了
使用matplotlib,同时在多个figure画图
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
使用场景:
我们使用matplotlib时,一次只在一个figure中画图,画完一个figure,然后创建另一个figure,再在新figure中画图。
这是我们如果想重新在旧的figure中画图,该如何做呢?本文就是为了解决这个需求。
方法
其实我们使用maplotlib的plot也好,imshow也好,这些画图方法其实并不是在figure中画图,而是在axes上画图。因此我们只需要保存旧的axes,然后当需要在旧的axes上画图时,将旧的axes置为当前axes。
import matplotlib.pyplot as pltplt.figure() ax1 = plt.gca() # 保存当前的axes# 在ax1上画图 plt.plot(...) plt.imshow(...) plt.figure() ax2 = plt.gca() # 保存当前的axes# 在ax2上画图 plt.plot(...) plt.imshow(...) plt.sca(ax1) # 将ax1置为当前axes# 在ax1上画图 plt.plot(...) plt.imshow(...) plt.sca(ax2) # 将ax2置为当前axes# 在ax2上画图 plt.plot(...) plt.imshow(...)总结
以上是生活随笔为你收集整理的使用matplotlib,同时在多个figure画图的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: scipy模块计算导数方法(centra
- 下一篇: Hermite曲线与Bezier曲线的关