防止 matplotlib 有状态
Prevent matplotlib statefulness
如果我在 matplotlib
中创建一个 Axes
对象并改变它(即通过绘制一些数据)然后我调用一个函数 而不传递我的 Axes
反对那个函数 那么那个函数仍然可以改变我的 Axes
。例如:
import matplotlib.pyplot as plt
import numpy as np
def innocent_looking_function():
#let's draw a red line on some unsuspecting Axes!
plt.plot(100*np.random.rand(20), color='r')
fig, ax = plt.subplots()
ax.plot(100*np.random.rand(20), color='b') #draw blue line on ax
#ax now has a blue line, as expected
innocent_looking_function()
#ax now unexpectedly has a blue line and a red line!
我的问题是:一般情况下我可以防止这种全局变量行为吗?我知道我可以在调用任何 innocent_looking_function()
之前调用 plt.close()
但有什么方法可以将其设置为默认值吗?
好的!您需要做的是在制作图形时完全绕过 pyplot
状态机。
它更冗长,因为您不能只调用 fig = plt.figure()
。
首先,让我解释一下 plt.gca()
或 plt.gcf()
是如何工作的。使用 pyplot
接口时,matplotlib 存储所有已创建但未显示的图形管理器。图管理器基本上是图的图形用户界面包装器。
plt._pylab_helpers.Gcf
是存储图形管理器并跟踪当前处于活动状态的图形管理器的单例对象。 plt.gcf()
returns 来自 _pylab_helpers.Gcf
的活跃人物。每个 Figure
对象都跟踪它自己的轴,所以 plt.gca()
只是 plt.gcf().gca()
.
通常,当您调用 plt.figure()
时,它:
- 创建返回的图形对象
- 使用适当的后端
为该图形创建一个FigureManager
- 图形管理器创建
FigureCanvas
、gui window(根据需要)和 NavigationToolbar2
(缩放按钮等)
- 图形管理器实例随后被添加到
_pylab_helpers.Gcf
的图形列表中。
这是我们要绕过的最后一步。
这是一个使用非交互式后端的简单示例。请注意,因为我们不担心与绘图交互,所以我们可以跳过整个图形管理器,只创建一个 Figure
和 FigureCanvas
实例。 (从技术上讲,我们可以跳过 FigureCanvas
,但一旦我们想要将绘图保存到图像等,就需要它)
import matplotlib.backends.backend_agg as backend
from matplotlib.figure import Figure
# The pylab figure manager will be bypassed in this instance. `plt.gca()`
# can't access the axes created here.
fig = Figure()
canvas = backend.FigureCanvas(fig)
ax = fig.add_subplot(111)
只是为了证明 gca
看不到这个轴:
import matplotlib.pyplot as plt
import matplotlib.backends.backend_agg as backend
from matplotlib.figure import Figure
# Independent figure/axes
fig = Figure()
canvas = backend.FigureCanvas(fig)
ax = fig.add_subplot(111)
ax.plot(range(10))
# gca() is completely unaware of this axes and will create a new one instead:
ax2 = plt.gca()
print 'Same axes?:', id(ax) == id(ax2)
# And `plt.show()` would show the blank axes of `ax2`
有了交互式支持,它会更复杂一些。你不能调用plt.show()
,所以你需要自己启动gui的主循环。您可以全部完成 "from scratch"(请参阅任何 "embedding matplotlib" 示例),但是 FigureManager
将支持的特定部分抽象掉:
以使用 TkAgg 后端为例:
import matplotlib.backends.backend_tkagg as backend
from matplotlib.figure import Figure
fig = Figure()
ax = fig.add_subplot(111)
manager = backend.new_figure_manager_given_figure(1, fig)
manager.show()
backend.show.mainloop()
要使用其他后端之一,只需更改后端导入即可。例如,对于 Qt4:
import matplotlib.backends.backend_qt4agg as backend
from matplotlib.figure import Figure
fig = Figure()
ax = fig.add_subplot(111)
manager = backend.new_figure_manager_given_figure(1, fig)
manager.show()
backend.show.mainloop()
这甚至可以与 IPython 笔记本中使用的 nbagg
后端一起使用。只需将后端导入更改为 import matplotlib.backends.backend_nbagg as backend
如果我在 matplotlib
中创建一个 Axes
对象并改变它(即通过绘制一些数据)然后我调用一个函数 而不传递我的 Axes
反对那个函数 那么那个函数仍然可以改变我的 Axes
。例如:
import matplotlib.pyplot as plt
import numpy as np
def innocent_looking_function():
#let's draw a red line on some unsuspecting Axes!
plt.plot(100*np.random.rand(20), color='r')
fig, ax = plt.subplots()
ax.plot(100*np.random.rand(20), color='b') #draw blue line on ax
#ax now has a blue line, as expected
innocent_looking_function()
#ax now unexpectedly has a blue line and a red line!
我的问题是:一般情况下我可以防止这种全局变量行为吗?我知道我可以在调用任何 innocent_looking_function()
之前调用 plt.close()
但有什么方法可以将其设置为默认值吗?
好的!您需要做的是在制作图形时完全绕过 pyplot
状态机。
它更冗长,因为您不能只调用 fig = plt.figure()
。
首先,让我解释一下 plt.gca()
或 plt.gcf()
是如何工作的。使用 pyplot
接口时,matplotlib 存储所有已创建但未显示的图形管理器。图管理器基本上是图的图形用户界面包装器。
plt._pylab_helpers.Gcf
是存储图形管理器并跟踪当前处于活动状态的图形管理器的单例对象。 plt.gcf()
returns 来自 _pylab_helpers.Gcf
的活跃人物。每个 Figure
对象都跟踪它自己的轴,所以 plt.gca()
只是 plt.gcf().gca()
.
通常,当您调用 plt.figure()
时,它:
- 创建返回的图形对象
- 使用适当的后端 为该图形创建一个
- 图形管理器创建
FigureCanvas
、gui window(根据需要)和NavigationToolbar2
(缩放按钮等) - 图形管理器实例随后被添加到
_pylab_helpers.Gcf
的图形列表中。
FigureManager
这是我们要绕过的最后一步。
这是一个使用非交互式后端的简单示例。请注意,因为我们不担心与绘图交互,所以我们可以跳过整个图形管理器,只创建一个 Figure
和 FigureCanvas
实例。 (从技术上讲,我们可以跳过 FigureCanvas
,但一旦我们想要将绘图保存到图像等,就需要它)
import matplotlib.backends.backend_agg as backend
from matplotlib.figure import Figure
# The pylab figure manager will be bypassed in this instance. `plt.gca()`
# can't access the axes created here.
fig = Figure()
canvas = backend.FigureCanvas(fig)
ax = fig.add_subplot(111)
只是为了证明 gca
看不到这个轴:
import matplotlib.pyplot as plt
import matplotlib.backends.backend_agg as backend
from matplotlib.figure import Figure
# Independent figure/axes
fig = Figure()
canvas = backend.FigureCanvas(fig)
ax = fig.add_subplot(111)
ax.plot(range(10))
# gca() is completely unaware of this axes and will create a new one instead:
ax2 = plt.gca()
print 'Same axes?:', id(ax) == id(ax2)
# And `plt.show()` would show the blank axes of `ax2`
有了交互式支持,它会更复杂一些。你不能调用plt.show()
,所以你需要自己启动gui的主循环。您可以全部完成 "from scratch"(请参阅任何 "embedding matplotlib" 示例),但是 FigureManager
将支持的特定部分抽象掉:
以使用 TkAgg 后端为例:
import matplotlib.backends.backend_tkagg as backend
from matplotlib.figure import Figure
fig = Figure()
ax = fig.add_subplot(111)
manager = backend.new_figure_manager_given_figure(1, fig)
manager.show()
backend.show.mainloop()
要使用其他后端之一,只需更改后端导入即可。例如,对于 Qt4:
import matplotlib.backends.backend_qt4agg as backend
from matplotlib.figure import Figure
fig = Figure()
ax = fig.add_subplot(111)
manager = backend.new_figure_manager_given_figure(1, fig)
manager.show()
backend.show.mainloop()
这甚至可以与 IPython 笔记本中使用的 nbagg
后端一起使用。只需将后端导入更改为 import matplotlib.backends.backend_nbagg as backend