IPython:如何在不同的单元格中显示相同的图? [英] IPython: How to show the same plot in different cells?

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

问题描述

一般来说,我还是IPython Notebook,Jupyter和Python的新手.

I'm still new to IPython Notebooks, Jupyter, and Python in general.

我正在使用以下代码在Jupyter笔记本中创建散点图:

I'm creating a scatter plot in a Jupyter notebook using the following code:

import numpy as np
import matplotlib.pyplot as plt

n = 1024
X = np.random.normal(0, 1, n)
Y = np.random.normal(0, 1, n)
plt.axes([0.025, 0.025, 0.95, 0.95])
plt.scatter(X, Y, s=50)

plt.show()

我的问题是,如何获得对绘图对象的引用,以便稍后可以在笔记本中的其他单元格中使用它?另外,在再次显示之前,可能需要修改该图.

My question is, how can I get a reference to the plot object so I can use it in a different cell later on in the notebook? Additionally, I may need to modify the plot before showing it again.

此外,我的笔记本顶部还有%matplotlib inline.

Also, I have %matplotlib inline at the top of my notebook.

以下是有关我的环境的一些信息:

Here are some info about my environment:

  • Python::3.5.2 64位[MSC v.1900 64位(AMD64)]
  • IPython: 4.2.0
  • numpy: 1.11.1
  • scipy: 0.17.1
  • matplotlib: 1.5.1
  • 符号: 1.0
  • 操作系统: Windows 7 6.1.7601 SP1
  • Python: 3.5.2 64bit [MSC v.1900 64 bit (AMD64)]
  • IPython: 4.2.0
  • numpy: 1.11.1
  • scipy: 0.17.1
  • matplotlib: 1.5.1
  • sympy: 1.0
  • OS: Windows 7 6.1.7601 SP1

推荐答案

我找到了解决方案!基本上,您使用fig, ax = plt.subplots()创建图形和轴,然后使用ax变量绘制(可能在多个单元格中绘制).在要重新绘制图形的任何单元格中,只需将fig写入单元格的最后一行,以使该单元格使用更新后的图形作为输出.
详情请参阅我的答案此处.

I have found a solution! Basically you create a figure and the axis with fig, ax = plt.subplots() and then use the ax variable to draw (potentially in multiple cells). In any of the cells you want to replot the figure, just write fig as the last line of the cell, resulting in the cell using the updated figure as output.
See my answer here for more details.

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

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