【问题标题】:How to capture a cell's output for use in another cell?如何捕获一个单元格的输出以用于另一个单元格?
【发布时间】:2020-12-08 05:34:21
【问题描述】:

在 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 有这样的功能吗?

【问题讨论】:

    标签: jupyter-notebook google-colaboratory


    【解决方案1】:

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

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

    关键位是:

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

    【讨论】:

    • 这令人失望,但我又被 org-mode 宠坏了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-08
    相关资源
    最近更新 更多