[英]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.