许多地块不需要交互,因此我尝试将其更改为嵌入式图。
我尝试了以下操作,但未成功:
plt.close(fig)
。清楚数字
plt.ioff()
,失败
在之间包装代码%matplotlib inline
%matplotlib notebook
。它关闭其他交互式地块
永远只有一个后端处于活动状态。可以更改后端,但这将需要关闭交互式图形。
一种选择是使用整个后端的交互式后端(例如%matplotlib widget
),并调用自定义函数,该函数可在需要时内联显示png图像。
#Cell1
%matplotlib widget
#Cell2
import matplotlib.pyplot as plt
def fig2inline(fig):
from IPython.display import display, Image
from io import BytesIO
plt.close(fig)
buff = BytesIO()
fig.savefig(buff, format='png')
buff.seek(0)
display(Image(data=buff.getvalue()))
#Cell3: (show the interactive plot)
fig, ax = plt.subplots(figsize=(3, 1.7))
ax.plot([1,3,4]);
#Cell4: (show the inline plot)
fig2, ax2 = plt.subplots(figsize=(3, 1.7))
ax2.plot([3,1,1]);
fig2inline(fig2)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句