![](/img/trans.png)
[英]matplotlib.legend.set_zorder() doesn't work in ipython notebook
[英]UnicodeDecodeError in IPython Notebook because of negative numbers in Matplotlib legend
在此處的小IPython筆記本中遇到了一個奇怪的錯誤: https : //gist.github.com/goulu/ba518d1a0a5267c0d3f3
在為IPython Notebook(2.7內核)生成svg圖的repr_svg方法中
plt.legend()
savefig(output, format='svg')
生成類似<!-- \\xe2\\x88\\x925 -->
圖例中的“ -5”,這會通過IPython / Jupyter客戶端在JSON中導致UnicodeDecodeError
錯誤在哪里? 在我的代碼中,在Matplotlib中還是在IPython中?
看來您的數據有一些不受支持的字符。 你可以嘗試
data= unicode(output.getvalue(), errors='replace')
要么
data= unicode(output.getvalue(), errors='ignore')
unicode('\xe2\x88\x925',errors= 'ignore')
output:
u'5'
通過在_repr_svg_方法的末尾添加.decode('utf-8')解決:
def _repr_svg_(self):
fig, ax = plt.subplots()
ax.plot(self.x,self.y)
plt.legend()
from io import BytesIO
output = BytesIO()
fig.savefig(output, format='svg')
data=output.getvalue() # .encode('utf-8') doesn't change anything
plt.close(fig)
return data.decode('utf-8')
對不起,噪音:-/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.