pet*_*tru 7 python weak-references matplotlib
我写了一个函数,绘制了一个由两个不同大小的子图组成的图:
def draw_plot(data, function, sigma_value):
gs = gridspec.GridSpec(1, 5)
ax1 = subplot(gs[0, 0:3])
ax2 = subplot(gs[0, 3:5], sharey=ax1)
gs.update(wspace=0.05)
...
Run Code Online (Sandbox Code Playgroud)
我应该提到这是一个模块级函数,因此在该模块的顶部我进行了导入
from pylab import *
import matplotlib.gridspec as gridspec
Run Code Online (Sandbox Code Playgroud)
当我跑步时myplot.draw_plot(...),我明白了RuntimeError.事情是这种行为是不一致的.我可以调用该函数,比方说,三次,前两次我得到错误,而第三次运行OK.
追溯是
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "myplot.py", line 105, in draw_plot
ax1 = subplot(gs[0, 0:3])
File "C:\Python32\lib\site-packages\matplotlib\pyplot.py", line 766, in subplot
a = fig.add_subplot(*args, **kwargs)
File "C:\Python32\lib\site-packages\matplotlib\figure.py", line 779, in add_subplot
a = subplot_class_factory(projection_class)(self, *args, **kwargs)
File "C:\Python32\lib\site-packages\matplotlib\axes.py", line 8380, in __init__
self._axes_class.__init__(self, fig, self.figbox, **kwargs)
File "C:\Python32\lib\site-packages\matplotlib\axes.py", line 467, in __init__
self.cla()
File "C:\Python32\lib\site-packages\matplotlib\axes.py", line 910, in cla
self._shared_y_axes.clean()
File "C:\Python32\lib\site-packages\matplotlib\cbook.py", line 1493, in clean
for key, val in mapping.items():
RuntimeError: dictionary changed size during iteration
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助!
编辑
显然我一直试图弄清楚自己发生了什么,所以跟着Traceback我检查了clean()功能cbook.py.
def clean(self):
"""
Clean dead weak references from the dictionary
"""
mapping = self._mapping
for key, val in mapping.items():
if key() is None:
del mapping[key]
val.remove(key)
Run Code Online (Sandbox Code Playgroud)
在函数中,我添加了一行可以打印mapping.items(),我注意到当这些项中有类似的条目时会发生错误<weakref at 0480EBA0; dead>.我完全不熟悉弱引用,所以我再次陷入困境.
编辑2
这当然不是一个好的解决方案,但注释掉clean()函数体有助于我的情况,而不会产生任何新的错误.
我刚刚发现最近的一篇文章Safely iterating over WeakKeyDictionary and WeakValueDictionary有助于解决非常类似的问题。
因此,使用 Bakuriu 给出的答案,我编辑了close()如下函数
def clean(self):
"""
Clean dead weak references from the dictionary
"""
mapping = self._mapping
for key, val in list(mapping.items()): # iterate over list now
if key() is None:
del mapping[key]
val.remove(key)
Run Code Online (Sandbox Code Playgroud)
它似乎工作得很好!
编辑
我刚刚发现matplotlib该函数的新版本如下所示:
def clean(self):
"""
Clean dead weak references from the dictionary
"""
mapping = self._mapping
to_drop = [key for key in mapping if key() is None]
for key in to_drop:
val = mapping.pop(key)
val.remove(key)
Run Code Online (Sandbox Code Playgroud)
来源:
https://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/cbook.py
| 归档时间: |
|
| 查看次数: |
1122 次 |
| 最近记录: |