AttributeError: __delete__ when saving figure in jupyter lab with %matplotlib widget
AttributeError: __delete__ when saving figure in jupyter lab with %matplotlib widget
在 jupyterlab(版本 2.2.2)上,如果我使用 %matplotlib widget
获取交互式图形,我在保存图形时会得到一个 AttributeError
。
这是一个示例代码:
%matplotlib widget
import matplotlib.pyplot as plt
import seaborn as sbs
sbs.set()
import pandas as pd
import numpy as np
import os
xy_test = pd.DataFrame({'FeatureX': np.arange(15), 'FeatureY': np.random.standard_normal(15)})
fig,ax = plt.subplots(1,1)
sbs.scatterplot(x='FeatureX', y='FeatureY', data=xy_test, ax=ax, s=70, palette='BrBG', legend='brief', edgecolor='k')
fig.tight_layout()
fig.suptitle('N=%d' %(xy_test.shape[0]));
flnm = os.path.join('testfig.png')
if not os.path.exists(flnm) or 1:
fig.savefig(flnm, bbox_inches='tight')
导致此错误消息:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-24-795a5ae23f02> in <module>
9 flnm = os.path.join('testfig.png')
10 if not os.path.exists(flnm) or 1:
---> 11 fig.savefig(flnm, bbox_inches='tight')
~\AppData\Local\Continuum\anaconda3\envs\python36\lib\site-packages\matplotlib\figure.py in savefig(self, fname, transparent, **kwargs)
2309 patch.set_edgecolor('none')
2310
-> 2311 self.canvas.print_figure(fname, **kwargs)
2312
2313 if transparent:
~\AppData\Local\Continuum\anaconda3\envs\python36\lib\site-packages\matplotlib\backend_bases.py in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, bbox_inches, pad_inches, bbox_extra_artists, backend, **kwargs)
2223 self.figure.set_edgecolor(origedgecolor)
2224 self.figure.set_canvas(self)
-> 2225 return result
2226
2227 @classmethod
~\AppData\Local\Continuum\anaconda3\envs\python36\lib\contextlib.py in __exit__(self, type, value, traceback)
86 if type is None:
87 try:
---> 88 next(self.gen)
89 except StopIteration:
90 return False
~\AppData\Local\Continuum\anaconda3\envs\python36\lib\site-packages\matplotlib\cbook\__init__.py in _setattr_cm(obj, **kwargs)
2064 for attr, orig in origs.items():
2065 if orig is sentinel:
-> 2066 delattr(obj, attr)
2067 else:
2068 setattr(obj, attr, orig)
AttributeError: __delete__
图形“保存”到磁盘,我可以在某些应用程序中打开它,例如 MS Paint,但不能在 Windows 照片应用程序中打开。使用照片时,我收到“文件系统错误 (-2147219196)”。
这不会发生在 %matplotlib notebook
。如何修复 %matplotlib widget
中的这个错误?
来自本期https://github.com/matplotlib/ipympl/issues/252
这个错误似乎是从 matplotlib 3.3.0 引入的。
所以目前,我的解决方案是将 matplotlib 降级到 3.2.2。
pip install matplotlib==3.2.2
在我的环境中一切正常。
此后发布了新版ipyml,可以继续matplotlib 3.3.x升级ipyml
pip install --upgrade ipympl
在 jupyterlab(版本 2.2.2)上,如果我使用 %matplotlib widget
获取交互式图形,我在保存图形时会得到一个 AttributeError
。
这是一个示例代码:
%matplotlib widget
import matplotlib.pyplot as plt
import seaborn as sbs
sbs.set()
import pandas as pd
import numpy as np
import os
xy_test = pd.DataFrame({'FeatureX': np.arange(15), 'FeatureY': np.random.standard_normal(15)})
fig,ax = plt.subplots(1,1)
sbs.scatterplot(x='FeatureX', y='FeatureY', data=xy_test, ax=ax, s=70, palette='BrBG', legend='brief', edgecolor='k')
fig.tight_layout()
fig.suptitle('N=%d' %(xy_test.shape[0]));
flnm = os.path.join('testfig.png')
if not os.path.exists(flnm) or 1:
fig.savefig(flnm, bbox_inches='tight')
导致此错误消息:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-24-795a5ae23f02> in <module>
9 flnm = os.path.join('testfig.png')
10 if not os.path.exists(flnm) or 1:
---> 11 fig.savefig(flnm, bbox_inches='tight')
~\AppData\Local\Continuum\anaconda3\envs\python36\lib\site-packages\matplotlib\figure.py in savefig(self, fname, transparent, **kwargs)
2309 patch.set_edgecolor('none')
2310
-> 2311 self.canvas.print_figure(fname, **kwargs)
2312
2313 if transparent:
~\AppData\Local\Continuum\anaconda3\envs\python36\lib\site-packages\matplotlib\backend_bases.py in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, bbox_inches, pad_inches, bbox_extra_artists, backend, **kwargs)
2223 self.figure.set_edgecolor(origedgecolor)
2224 self.figure.set_canvas(self)
-> 2225 return result
2226
2227 @classmethod
~\AppData\Local\Continuum\anaconda3\envs\python36\lib\contextlib.py in __exit__(self, type, value, traceback)
86 if type is None:
87 try:
---> 88 next(self.gen)
89 except StopIteration:
90 return False
~\AppData\Local\Continuum\anaconda3\envs\python36\lib\site-packages\matplotlib\cbook\__init__.py in _setattr_cm(obj, **kwargs)
2064 for attr, orig in origs.items():
2065 if orig is sentinel:
-> 2066 delattr(obj, attr)
2067 else:
2068 setattr(obj, attr, orig)
AttributeError: __delete__
图形“保存”到磁盘,我可以在某些应用程序中打开它,例如 MS Paint,但不能在 Windows 照片应用程序中打开。使用照片时,我收到“文件系统错误 (-2147219196)”。
这不会发生在 %matplotlib notebook
。如何修复 %matplotlib widget
中的这个错误?
来自本期https://github.com/matplotlib/ipympl/issues/252
这个错误似乎是从 matplotlib 3.3.0 引入的。
所以目前,我的解决方案是将 matplotlib 降级到 3.2.2。
pip install matplotlib==3.2.2
在我的环境中一切正常。
此后发布了新版ipyml,可以继续matplotlib 3.3.x升级ipyml
pip install --upgrade ipympl