python - Matplotlib 的 "interactive mode"(ion(), ioff()) 的确切语义?

标签 python matplotlib interactive-mode

Matplotlib 的 pyplot 中“交互模式”的文档如下:

The interactive property of the pyplot interface controls whether a figure canvas is drawn on every pyplot command. If interactive is False, then the figure state is updated on every plot command, but will only be drawn on explicit calls to draw(). When interactive is True, then every pyplot command triggers a draw.

这似乎很清楚:当交互模式打开时,可以执行 plot() 而无需执行 draw()。但是,在下面的代码中执行 draw() 并没有做任何事情:

from matplotlib import pyplot as pp

# Interactive mode is off by default

pp.plot([10, 20, 50])
pp.draw()

raw_input('Press enter...')  # No graph displayed?!!

(在 Windows XP 上,Matplotlib 1.0.1)。

在开头添加 ion() 使图形出现,同时等待用户输入回车(方便关闭所有图形):

from matplotlib import pyplot as pp

ion()

pp.plot([10, 20, 50])  # No draw() is necessary

raw_input('Press enter...')  # The graph is interactive *and* the terminal responds to enter

因此,看起来 ion() 所做的更多 不仅仅是在每个绘图命令之后添加自动图形更新,不幸的是我在文档中找不到任何内容。后一个程序的另一个更重要的问题是 ion() 使所有绘图命令都更新图形,当单个图形被多次更新时,这很耗时。

那么,有没有办法:

  • 让终端等待进入,然后自动关闭所有图形,
  • 拥有交互式 Matplotlib 图表,
  • ...不强制在开始时打开交互模式(以免强制自动更新图表,这可能很耗时)?

最佳答案

这里是 interesting discussion 的摘要Matplotlib 邮件列表中的这个主题。执行摘要是:

  • 交互模式(使用 ion() 激活)可以自动完成很多事情。特别是,pyplot.* 命令会自动在屏幕上更新相关轴。但是,对 Matplotlib 对象(如 ax.plot() (ax 是一个 Axes 对象)的方法调用通常不会执行自动更新;在这种情况下,pyplot.draw() 执行必要的更新。)

  • 非交互模式不太方便。 draw() not 通常会更新屏幕上的图形。当前文档中没有提到 draw() 在非交互模式下有点“不活跃”的事实,但希望很快就会包含在内。

同时,关于交互和非交互模式的更多信息可以在 a current branch 中找到。 Matplotlib 的。更好的documentation对于draw()show()和好友也可以在同一个分支中找到。

关于python - Matplotlib 的 "interactive mode"(ion(), ioff()) 的确切语义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6130341/

相关文章:

bash - 即使我有一个应该阻止它的陷阱 INT,我的交互式 bash 脚本循环也会中断?

python - 标准标签的 PyYAML 替代构造函数

python - 如何在matplotlib中删除一刻度的轴标签

python - matplotlib:分组箱线图

Grails 默认包名

ruby , pry 开: Can I add something to the command `pry example.rb` so pry automatically goes interactive when it finishes executing the script?

python - 读取文件的文件内容并在发生异常时设置为空字符串的最佳方法

python - 如何从字典中打印特定的键值?

python - 在选择列 Pandas 中选择值相同的行?

python - 设置 `ylabel`的位置