我试图弄清楚为什么使用此代码显示了一个额外的空白图形,应该只有两个图形,但其中三个显示为空白。这是显示发生情况的图像:
这是我正在使用的代码:
f1 = plt.figure(1)
img=mpimg.imread(image_path)
imgplot = plt.imshow(img)
original_image = Image.open(image_path)
bw_image = original_image.convert('1', dither=Image.NONE)
bw_image_array = np.array(bw_image, dtype=np.int)
black_indices = np.argwhere(bw_image_array == 0)
chosen_black_indices = black_indices[np.random.choice(black_indices.shape[0], replace=False, size=3000)]
distances = pdist(chosen_black_indices)
distance_matrix = squareform(distances)
optimized_path = solve_tsp(distance_matrix)
optimized_path_points = [chosen_black_indices[x] for x in optimized_path]
f2 = plt.figure(2)
plt.xlim(0, 600)
plt.ylim(0, 800)
plt.gca().invert_yaxis()
plt.xticks([])
plt.yticks([])
xstartdata = optimized_path_points[0][0]
ystartdata = optimized_path_points[0][1]
fig, ax = plt.subplots()
ln, = plt.plot([], [], color='black', marker='o', linestyle='dashed', markersize=1, animated=True)
plt.plot([xstartdata], [ystartdata], color='black', marker='o', linestyle='dashed', markersize=1, lw=0.1)
plt.gca().invert_yaxis()
x = []
y = []
def init():
ln.set_data([],[])
return ln,
def animate(i):
x.append(optimized_path_points[i][1])
y.append(optimized_path_points[i][0])
ln.set_data(x, y)
plt.plot(x , y , lw=0.1,animated=True)
return ln,
anim = animation.FuncAnimation(fig, animate, init_func=init,frames=range(len(optimized_path_points)), interval=5, blit=True)
plt.show()