|
| 1 | +# 在 Python shell 中使用 Matplotlib |
| 2 | + |
| 3 | +> 原文:[Using matplotlib in a python shell](http://matplotlib.org/users/shell.html) |
| 4 | +
|
| 5 | +> 译者:[飞龙](https://github.com/) |
| 6 | +
|
| 7 | +> 协议:[CC BY-NC-SA 4.0](http://creativecommons.org/licenses/by-nc-sa/4.0/) |
| 8 | +
|
| 9 | +> 警告 |
| 10 | +
|
| 11 | +> 该页面的内容已严重过时。 |
| 12 | +
|
| 13 | +默认情况下,matplotlib 将绘图延迟到脚本结束,因为绘图可能是开销大的操作,并且你可能不想在每次更改单个属性时更新绘图,而是只在所有属性更改后更新一次。 |
| 14 | + |
| 15 | +但是在 python shell 中工作时,通常需要用每个命令更新绘图,例如,在更改`xlabel()`或一行的标记样式之后。 虽然这在概念上很简单,但在实践中它可能很棘手,因为 matplotlib 在底层是一个图形用户界面应用程序,并拥有一些技巧,使应用程序在一个 python shell 正常工作。 |
| 16 | + |
| 17 | +## 使用 IPython 解决 |
| 18 | + |
| 19 | +> 注意 |
| 20 | +
|
| 21 | +> 这里描述的模式出于历史原因仍然存在,但强烈建议不要使用。它污染函数的命名空间,会影响 python 内建设施,并可能导致错误难以跟踪。 要获得 IPython 集成而无需导入,使用`%matplotlib`魔术命令是首个选择。 参见 [ipython 文档](http://ipython.org/ipython-doc/stable/interactive/reference.html#plotting-with-matplotlib)。 |
| 22 | +
|
| 23 | +幸运的是,一个增强的交互式 python shell,[ipython](http://ipython.org/) 已经找出了所有这些技巧,并且可被 matplotlib 感知,所以当你在 pylab 模式下启动 ipython。 |
| 24 | + |
| 25 | +``` |
| 26 | +johnh@flag:~> ipython |
| 27 | +Python 2.4.5 (#4, Apr 12 2008, 09:09:16) |
| 28 | +IPython 0.9.0 -- An enhanced Interactive Python. |
| 29 | +
|
| 30 | +In [1]: %pylab |
| 31 | +
|
| 32 | + Welcome to pylab, a matplotlib-based Python environment. |
| 33 | + For more information, type 'help(pylab)'. |
| 34 | +
|
| 35 | +In [2]: x = randn(10000) |
| 36 | +
|
| 37 | +In [3]: hist(x, 100) |
| 38 | +``` |
| 39 | + |
| 40 | +它为你设置一切使交互式绘图工作,就像你期望的那样。 调用`figure()`并弹出图形窗口,调用`plot()`使你的数据出现在图形窗口中。 |
| 41 | + |
| 42 | +注意在上面的例子中,我们没有导入任何 matplotlib 名称,因为在 pylab 模式下,ipython 将自动导入它们。 ipython 还为你启用交互模式,这会导致每个 pyplot 命令触发图形更新,并且还提供了一个 matplotlib 感知的运行命令,来高效运行 matplotlib 脚本。 ipython 在运行命令期间关闭交互模式,然后在运行结束时恢复交互状态,以便你可以手动继续调整图形。 |
| 43 | + |
| 44 | +ipython 已经嵌入了很多最近的作品,从 pylab 支持,到各种 GUI 应用程序,所以请检查 ipython 邮件列表的最新状态。 |
| 45 | + |
| 46 | +## 其它 Python 解释器 |
| 47 | + |
| 48 | +如果你不能使用 ipython,并且仍然想在交互式 python shell 使用 matplotlib/pylab,例如,plain-ole 标准的 python 交互式解释器,你将需要了解[什么是 matplotlib 后端](http://matplotlib.org/faq/usage_faq.html#what-is-a-backend) 。 |
| 49 | + |
| 50 | +有了 TkAgg 后端,它使用 Tkinter 用户界面工具包,你可以从任意的非 gui python shell 使用 matplotlib。 只需在你的`matplotlibrc`文件中设置`backend : TkAgg `和`interactive : True `(请参阅自定义 matplotlib)并启动 python。 然后: |
| 51 | + |
| 52 | +```py |
| 53 | +>>> from pylab import * |
| 54 | +>>> plot([1,2,3]) |
| 55 | +>>> xlabel('hi mom') |
| 56 | +``` |
| 57 | + |
| 58 | +应该能够开箱即用。 这也可能适用于最新版本的 qt4agg 和 gtkagg 后端,以及 Macintosh 上的 macosx 后端。 注意,在批处理模式下,即从脚本制作图形时,交互模式可能很慢,因为它用每个命令重绘图形。 因此,你可能需要仔细考虑,然后通过`matplotlibrc`文件而不是使用下一节中列出的函数,使其作为默认行为。 |
| 59 | + |
| 60 | +Gui shell 问题最多,因为它们必须运行主循环,但是交互式绘图也涉及主循环。 Ipython 已经为 matplotlib 主后端解决了这一切问题。 可能有其他 shell 和 IDE 也可以在交互模式下使用 matplotlib,但一个明显的候选项不会:python IDLE IDE 是一个不支持 pylab 交互模式的 Tkinter gui 应用程序,无论后端是什么。 |
| 61 | + |
| 62 | +## 控制交互式更新 |
| 63 | + |
| 64 | +`pyplot`接口的`interactive`属性控制是否在每个`pyplot`命令上绘制图画布。 如果`interactive`是`False`,那么每个`plot`命令都会更新图形状态,但只会在显式调用`draw()`时绘制。 当`interactive`为`True`时,每个`pyplot`命令都会触发绘制。 |
| 65 | + |
| 66 | +`pyplot`接口提供了 4 个有助于交互式控制的命令。 |
| 67 | + |
| 68 | +`isinteractive()` |
| 69 | + |
| 70 | +返回交互式设置。`True|False`。 |
| 71 | + |
| 72 | +`ion()` |
| 73 | + |
| 74 | +将交互式模式打开。 |
| 75 | + |
| 76 | +`ioff()` |
| 77 | + |
| 78 | +将交互式模式关闭。 |
| 79 | + |
| 80 | +`draw()` |
| 81 | + |
| 82 | +强制图形重新绘制。 |
| 83 | + |
| 84 | +当处理绘图开销很大的大型图形时,你可能希望临时关闭 matplotlib 的交互式设置来避免性能损失: |
| 85 | + |
| 86 | +```py |
| 87 | +>>> #create big-expensive-figure |
| 88 | +>>> ioff() # turn updates off |
| 89 | +>>> title('now how much would you pay?') |
| 90 | +>>> xticklabels(fontsize=20, color='green') |
| 91 | +>>> draw() # force a draw |
| 92 | +>>> savefig('alldone', dpi=300) |
| 93 | +>>> close() |
| 94 | +>>> ion() # turn updating back on |
| 95 | +>>> plot(rand(20), mfc='g', mec='r', ms=40, mew=4, ls='--', lw=3) |
| 96 | +``` |
0 commit comments