簡體   English   中英

混合 matplotlib 交互式和內聯圖?

[英]Mix matplotlib interactive and inline plots?

許多繪圖不需要是交互式的,所以我嘗試將它們更改為內嵌繪圖。

我嘗試了以下但沒有成功:

  • 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)

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM