matplotlib - jupyter notebook 中的交互式 matplotlib 绘图

标签 matplotlib ipython jupyter jupyter-notebook

以下代码段在 ipython 控制台中按预期工作:

> anaconda3/bin/ipython3
In [1]: import matplotlib.pyplot as plt
        import pandas as pd
        import numpy as np
In [2]: plt.ion()
In [3]: pd.Series(np.sin(np.arange(0, 10, 0.1))).plot() # plot window appears
In [4]: pd.Series(np.cos(np.arange(0, 10, 0.1))).plot() # second line is drawn in the same window

终端在任何时候都不会被阻塞。如何在 Jupyter Notebook 中获得相同的行为?也就是说,可以从笔记本逐步绘制的外部交互式绘图窗口。

相同的代码段不显示笔记本中的绘图。执行 plt.show() 将显示外部窗口,但会阻止执行直到窗口关闭。

提前致谢。

最佳答案

即使不需要后端开关,笔记本中也需要 %matplotlib 魔法,之后笔记本的行为与控制台相同。例如,将其作为笔记本中的第一个单元格执行:

%matplotlib
import matplotlib.pyplot as plt
plt.ion()

关于matplotlib - jupyter notebook 中的交互式 matplotlib 绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39428347/

相关文章:

python - 使用 groupby 时迭代列以生成条形图

ssl - ipython 笔记本服务器 SSL 错误

pip - 使用 pip 安装 iPython Notebook

IPython笔记本: Can I check if there is a server active for a particular directory?

markdown - jupyter笔记本中的编号标题

python - 基于jupyter笔记本创建项目

matplotlib - 如何在 matplotlib 中自定义标记/符号属性?

python - Matplotlib:如何更改双条形图的图大小

Python:上标仅打印 float 的开头

python - 将字符串作为变量而不是可执行代码片段粘贴到 IPython 中