如何叠加来自不同单元格的图? [英] How to overlay plots from different cells?

查看:87
本文介绍了如何叠加来自不同单元格的图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在笔记本中的一个单元格中用

In one of the cells in my notebook, I already plotted something with

myplot = plt.figure() plt.plot(x,y)

myplot = plt.figure() plt.plot(x,y)

现在,在另一个单元格中,我想再次绘制完全相同的图形,但是在其上面添加新的图形(类似于两次连续调用plt.plot()会发生的情况).我尝试在新单元格中添加以下内容:

Now, in a different cell, I'd like to plot the exact same figure again, but add new plots on top of it (similar to what happens with two consecutive calls to plt.plot()). What I tried was adding the following in the new cell:

myplot plt.plot(xnew,ynew)

myplot plt.plot(xnew,ynew)

但是,我在新单元格中获得的唯一内容是新情节,没有前一个情节.

However, the only thing I get in the new cell is the new plot, without the former one.

一个人怎么能做到这一点?

How can one achieve this?

推荐答案

基本上有两种方法可以解决此问题.

There are essentially two ways to tackle this.

使用面向对象的方法,即保持图形和/或轴的句柄并在以后的单元格中重新使用它们.

Use the object-oriented approach, i.e. keep handles to the figure and/or axes and reuse them in later cells.

import matplotlib.pyplot as plt
%matplotlib inline

fig, ax=plt.subplots()
ax.plot([1,2,3])

然后在以后的单元格中,

Then in a later cell,

ax.plot([4,5,6])

建议阅读:

另一种选择是告诉matplotlib内联后端使数字在单元格末尾保持打开状态.

The other option is to tell the matplotlib inline backend to keep the figures open at the end of a cell.

import matplotlib.pyplot as plt
%matplotlib inline

%config InlineBackend.close_figures=False # keep figures open in pyplot

plt.plot([1,2,3])

然后在以后的单元格中

plt.plot([4,5,6])

建议阅读:

在IPython笔记本中处理内嵌图形

这篇关于如何叠加来自不同单元格的图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆