使用相同的python脚本将两个图像保存到两个单独的文件中

使用相同的python脚本将两个图像保存到两个单独的文件中,python,Python,如何使用相同的python脚本将两个图像保存到两个单独的文件中。两个输出图像是相关矩阵和一个图。我用的是matplotlib imshow(matrix, interpolation='bilinear') colorbar() savefig('/home/sudipta/Downloads/phppython/'+ filename +'.png') x = range(-width, width) plt.plot(x, avg_vec) plt.savefig('/home/sudipt

如何使用相同的python脚本将两个图像保存到两个单独的文件中。两个输出图像是相关矩阵和一个图。我用的是matplotlib

imshow(matrix, interpolation='bilinear')
colorbar()
savefig('/home/sudipta/Downloads/phppython/'+ filename +'.png')
x = range(-width, width)
plt.plot(x, avg_vec)
plt.savefig('/home/sudipta/'+ filename +'plot' +'.png')

当我运行此脚本时,这些数字相互重叠。但是,我需要两个单独的图像。最初,我试图将这些图像保存在同一个目录中。我想可能有问题。然后,我试图保存在另一个目录中。但是,我没有成功

我认为您应该在两者之间添加pyplot.clf()

#plot your first image
pyplot.savefig('filename.ext') # ext is your chosen filetype. jpg, png, etc.

#plot your second image
pyplot.savefig('filename2.ext')
#plot your first image
pyplot.savefig('filename.ext') # ext is your chosen filetype. jpg, png, etc.

# clear the plot
pyplot.clf()

# plot your second image
pyplot.savefig('filename2.ext')
函数clf(清除图形)删除当前轴上的所有图形

另一个选项是创建两个不同的地物对象

# create figures and axes
fig0 = pyplot.figure()
ax0 = fig0.add_subplot(1, 1, 1)
fig1 = pyplot.figure()
ax1 = fig1.add_subplot(1, 1, 1)

# draw on ax0, e.g. with ax0.plot(...)
# draw on ax1

fig0.savefig('fig0.png')
fig1.savefig('fig1.png')

你能澄清这个问题吗?这些图像的来源是什么?拯救他们有什么困难?一些代码可能会在这里有所帮助!我不知道怎么做。请在这方面帮助我