首页 > 解决方案 > jupyter & matplotlib - 如何打开和关闭图形上的交互模式

问题描述

问题

在 Jupyter notebook 中,是否可以关闭图形上的交互模式?ax.plot()从命令行 python 运行时不显示一行,但在 Jupyter 笔记本单元格中,它显示了。

import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(7,5))
plt.ioff()
ax.plot([1.6, 2.7])     # <--- Still the line shows up in a jupyter notebook

...

plt.ion()
plt.draw()
plt.show()

版本

$ jupyter notebook --version
6.2.0

from platform import python_version
print(python_version())

3.8.8

有关的

matplotlib python inline on/off建议使用%matplotlib但不知道为什么使用魔法命令来打开/关闭交互模式。

它还在下面提到,但不确定它的确切含义。

on 和 ioff 设置交互模式,确定是否在每个绘图命令后更新绘图(交互模式打开)或等待 plt.show()(如果交互模式关闭)。这独立于内联,它决定笔记本或控制台是否应该显示图形(这会在绘图更新时发生,这意味着如果使用 ion 设置交互模式,则在每个绘图命令中,或者如果使用 ioff 未设置,则在 plt.plot 之后)。

标签: matplotlibjupyter-notebook

解决方案


推荐阅读