简体   繁体   English

如何防止 matplotlib 在 jupyter notebook 中使用 plt.close() 显示图形

[英]How to prevent matplotlib from to showing a figure even with plt.close() in jupyter notebook

I create a matplotlib figure within my neural network training loop for creating a Tensorboard image.我在我的神经网络训练循环中创建了一个 matplotlib 图形,用于创建 Tensorboard 图像。 Therefore, I use plot_to_image() to convert the figure to an image as mentioned in the official Tensorboard guide .因此,我使用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..')
    # ...

The plt.close(figure) should prevent the figure to be shown. plt.close(figure)应该防止显示图形。 However, in my jupyter notebook, I get an empty space in my output between the print statements.但是,在我的 jupyter 笔记本中,我在打印语句之间的 output 中有一个空白区域。 If I highlight the output, I can even see the three images I create for each epoch as a blank space (Yes, I call the function three different times for one epoch for different figures).如果我突出显示 output,我什至可以将我为每个时期创建的三个图像视为一个空白区域(是的,我将 function 称为一个时期的三个不同时间,用于不同的数字)。

在此处输入图像描述

My question now is:我现在的问题是:
How can I change this behaviour but still get my print statement shown?如何更改此行为但仍显示我的打印语句?
Thanks in advance提前致谢

Managed to solve my problem by using plt.ioff() at the start of my training routine and plt.ion() at the end.通过在我的训练程序开始时使用plt.ioff()plt.ion()使用 plt.ion() 设法解决了我的问题。 The answer was given in a comment of the question linked by @TFer.答案在@TFer 链接的问题的评论中给出。 Thanks!谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 jupyter notebook - matplotlib 显示图形,即使没有调用 plt.show() - jupyter notebook - matplotlib shows figure even without calling plt.show() Jupyter 笔记本:即使在“from matplotlib import pyplot”之后,如何持续设置 matplotlib 图形默认大小? - Jupyter notebook: How to set matplotlib figure default size persistently, even after `from matplotlib import pyplot`? 数字大小不响应我在我的jupyter笔记本中使用matplotlib的plt.figure()中的figsize设置 - figure size does not respond to my figsize setting in plt.figure() with matplotlib in my jupyter notebook 从 tkinter window 调用时使用 plt.close('all') 后 plt.show() 冻结 - plt.show() freezes after using plt.close('all') when calling from tkinter window plt.close('all') Python 在 Spyder 中不起作用 - plt.close('all') Python not working in Spyder 防止情节显示在 jupyter notebook 中 - prevent plot from showing in jupyter notebook 在 jupyter notebook 中显示来自自定义类的 matplotlib 图 - displaying a matplotlib figure from a custom class in jupyter notebook plt.close() 和 plt.clf() 之间的区别 - Difference between plt.close() and plt.clf() 如何在 Jupyter Notebook 的不同单元格中更改 matplotlib 图形? - How to change a matplotlib figure in a different cell in Jupyter Notebook? 如何在 Jupyter 笔记本中重绘 matplotlib.pyplot 图? - How do I redraw a matplotlib.pyplot figure in a Jupyter notebook?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM