![](/img/trans.png)
[英]jupyter notebook - matplotlib shows figure even without calling plt.show()
[英]How to prevent matplotlib from to showing a figure even with plt.close() in jupyter notebook
我在我的神經網絡訓練循環中創建了一個 matplotlib 圖形,用於創建 Tensorboard 圖像。 因此,我使用plot_to_image()
將圖形轉換為官方 Tensorboard 指南中提到的圖像。
def plot_to_image(figure):
"""Converts the matplotlib plot specified by 'figure' to a PNG image and
returns it. The supplied figure is closed and inaccessible after this call."""
# Save the plot to a PNG in memory.
buf = io.BytesIO()
plt.savefig(buf, format='png')
# Closing the figure prevents it from being displayed directly inside the notebook.
plt.close(figure)
buf.seek(0)
# Convert PNG buffer to TF image
image = tf.image.decode_png(buf.getvalue(), channels=3)
# Add the batch dimension
image = tf.expand_dims(image, 0)
return image
for epoch in range(n_epochs):
# ...
# error_fig = >some fancy plt.fig for visualizing stuff in Tensorboard<
tf.summary.image(name='train_error_img', data=plot_to_image(error_fig), step=epoch)
print('Some metrics for this particular epoch..')
# ...
plt.close(figure)
應該防止顯示圖形。 但是,在我的 jupyter 筆記本中,我在打印語句之間的 output 中有一個空白區域。 如果我突出顯示 output,我什至可以將我為每個時期創建的三個圖像視為一個空白區域(是的,我將 function 稱為一個時期的三個不同時間,用於不同的數字)。
我現在的問題是:
如何更改此行為但仍顯示我的打印語句?
提前致謝
通過在我的訓練程序開始時使用plt.ioff()
和plt.ion()
使用 plt.ion() 設法解決了我的問題。 答案在@TFer 鏈接的問題的評論中給出。 謝謝!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.