Matplotlib的“交互模式"的确切语义(ion(),ioff())? [英] Exact semantics of Matplotlib's "interactive mode" (ion(), ioff())?

查看:123
本文介绍了Matplotlib的“交互模式"的确切语义(ion(),ioff())?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Matplotlib的pyplot中的​​交互模式"文档如下:

The documentation for the "interactive mode" in Matplotlib's pyplot reads:

pyplot界面的交互属性控制是否在每个pyplot命令上绘制图形画布.如果interactive为False,则在每个绘图命令上都会更新图形状态,但只会在显式调用draw()时绘制图形状态.当Interactive为True时,则每个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.

这似乎很清楚:启用交互模式后,无需执行draw()就可以执行plot().但是,在以下代码中执行draw()不会执行任何操作:

This seems clear enough: when the interactive mode is on, one can do plot() without having to do draw(). However, doing draw() in the following code does not do anything:

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).

(on Windows XP, Matplotlib 1.0.1).

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

Adding ion() at the beginning makes the figure(s) appear, while waiting for the user to type enter (which conveniently closes all the figures):

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()使所有绘图命令都更新图形,这在单个图形被多次更新时很费时间.

Thus, it looks like ion() does more than just adding automatic graph updates after each plotting command, and I unfortunately can't find anything in the documentation. Another, more important problem with the latter program is that ion() makes all plot commands update the graph, which is time consuming when a single graph is updated multiple times.

那么,有没有办法:

  • 让终端等待输入,然后所有数字自动关闭,
  • 具有交互式Matplotlib图
  • …无需在开始时强制启用交互模式(以免强制自动更新图表,这可能很耗时)?

推荐答案

以下是

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

    • The interactive mode (activated with ion()) automates many things. In particular, pyplot.* commands automatically update on the screen the relevant axes. However, method calls on Matplotlib objects like ax.plot() (ax being an Axes object) do not normally perform automatic updates; in this case, pyplot.draw() performs the necessary update.)

    非交互模式不太方便. draw()不会通常会更新屏幕上的图形.在当前文档中没有提到draw()在非交互模式下处于非活动状态"这一事实,但希望很快会被包含在该文档中.

    The non-interactive mode is less convenient. draw() does not normally update the figure on screen. The fact that draw() is somewhat "inactive" in non-interactive mode is not mentioned in the current documentation, but will hopefully be included there soon.

    同时,可以在文档.同一分支.

    In the mean time, more information on the interactive and non-interactive modes can be found in a current branch of Matplotlib. A better documentation for draw(), show() and friends can also be found in the same branch.

    这篇关于Matplotlib的“交互模式"的确切语义(ion(),ioff())?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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