如何捕获一个单元格的输出以用于另一个单元格?

How to capture a cell's output for use in another cell?

在 org-mode 中,我可以命名代码块的输出并将其包含在文档的其他位置。
是否可以在 colab .ipynb 文件或一般的 Jupyter 笔记本中执行此操作(或类似操作)?

例如,我可以用一些 Python 代码生成一个单元格来生成一个图:

import matplotlib.pyplot as plt
plt.plot([0,2,1,4,9])

执行后,绘图出现在代码单元格下方。到目前为止一切顺利。
但是我如何捕捉这个图并在笔记本的其他地方使用它?

我希望有一些语法,以便我可以将绘图包含在降价单元格中,例如:

# this is my title
As you can see, the numbers go up and down in the plot:

![][some_magic_link_here]

Here is some more text to explain the plot.

colab中有这样的功能吗?

好消息 - 在另一个降价单元格中嵌入图像是自助服务。这是一个例子:

完整笔记本: https://colab.research.google.com/drive/1PF-hT-m8eZN2CBzFkMp9Bvzj9pSBQVYn

关键位是:

  1. 使用 IPython.display.Markdown 是为了以编程方式构建降价。
  2. 除渲染外,使用savefig保存matplotlib图形。
  3. base64编码后将图像数据包含在markdown中。