Program window flashing problem

The following problems were found during the process of generating the heat map debugging code:

import matplotlib.pyplot as plt
import cv2
import numpy as np
import torch

# 生成一个二维数组作为示例数据
data = np.random.rand(10, 10)

# 使用imshow函数显示热力图
plt.imshow(data, cmap='hot', interpolation='nearest')

# 添加颜色栏
plt.colorbar()
# 显示图像,并阻塞程序执行
plt.show()

The above code can generate a heat map, and the program window does not flash by, while the following code is the generated program window flash by:

import matplotlib.pyplot as plt
import cv2
import numpy as np

# 生成一个二维数组作为示例数据
data = np.random.rand(10, 10)

# 使用imshow函数显示热力图
plt.imshow(data, cmap='hot', interpolation='nearest')

# 添加颜色栏
plt.colorbar()
# 显示图像,并阻塞程序执行
plt.show()

It is not difficult to find that the code is only missing one import torch , but it produces two completely different effects, and it can be seen that this import torch is not used:
insert image description here
after Baidu found out the reason, this is because the import torch statement is used in the code , the import of the torch module will introduce some background threads or settings related to the graphics display, which will cause the graphics window to remain open. Without import torch, the necessary configuration or threads related to the graphics display may be missing, so the graphics The window flashes by, but the import torch statement itself should not directly affect the display of the graphics window. It only performs related initialization operations when importing the torch module, which may be indirectly related to the graphics display environment.

But once the import torch program window is missing, it will still flash by. I don’t know if anyone knows the reason. Please leave a message in the comment area to communicate.

Guess you like

Origin blog.csdn.net/qq_45104603/article/details/131195653