我通过保存一系列图片来制作电影,然后使用ImageJ将它们放在一起,如下所示:
for i in range(n):
fname = 'out_' + str(1000+i)
plt.imsave(fname, A[i], cmap=cmapA)
我想以某种方式“连接”两个图像(例如它们并排显示)并保存,使用不同的颜色图生成。所以假设:
for i in range(n):
fname = 'out_' + str(1000+i)
plt.hypothetical_imsave(fname, (A[i], B[i]), cmap=(cmapA, cmapB), axis=1)
当然,这是伪代码,但是有没有办法用好旧的numpy和matplotlib来做这个而不安装一个全新的包?
答案 0 :(得分:3)
不是像我上面的回答那样创建两个中间图像,另一种方法是使用imshow
制作两个数组,如下例所示。
我们将使用imshow
从._rgbacache
对象中获取numpy数组,但要生成此数组,您必须在轴上显示imshow
对象,因此fig
和ax
开头。
import numpy as np
import matplotlib.pyplot as plt
# Need to display the result of imshow on an axis
fig=plt.figure()
ax=fig.add_subplot(111)
# Save fig a with one cmap
a=np.random.rand(20,20)
figa=ax.imshow(a,cmap='jet')
# Save fig b with a different cmap
b=np.random.rand(20,20)
figb=ax.imshow(a,cmap='copper')
# Have to show the figure to generate the rgbacache
fig.show()
# Get the array of data
figa=figa._rgbacache
figb=figb._rgbacache
# Stitch the two arrays together
figc=np.concatenate((figa,figb),axis=1)
# Save without a cmap, to preserve the ones you saved earlier
plt.imsave('figc.png',figc,cmap=None)
修改强>
要使用imsave
和类似文件的对象执行此操作,您需要cStringIO
:
import numpy as np
import matplotlib.pyplot as plt
from cStringIO import StringIO
s=StringIO()
t=StringIO()
# Save fig a with one cmap to a StringIO instance. Need to explicitly define format
a=np.random.rand(20,20)
plt.imsave(s,a,cmap='jet',format='png')
# Save fig b with a different cmap
b=np.random.rand(20,20)
plt.imsave(t,b,cmap='copper',format='png')
# Return to beginning of string buffer
s.seek(0)
t.seek(0)
# Get the array of data
figa=plt.imread(s)
figb=plt.imread(t)
# Stitch the two arrays together
figc=np.concatenate((figa,figb),axis=1)
# Save without a cmap, to preserve the ones you saved earlier
plt.imsave('figc.png',figc,cmap=None)
答案 1 :(得分:2)
您可以使用imsave
首先使用所需的cmap
保存图像,然后使用imread
,concatenate
数组重新打开它们,然后使用{{1再次保存连结的数字,imsave
设置为cmap
None
图a,import matplotlib.pyplot as plt
import numpy as np
# Save fig a with one cmap
a=np.random.rand(200,200)
plt.imsave('figa.png',a,cmap='jet')
# Save fig b with a different cmap
b=np.random.rand(200,200)
plt.imsave('figb.png',a,cmap='copper')
# Reopen fig a and fig b
figa=plt.imread('figa.png')
figb=plt.imread('figb.png')
# Stitch the two figures together
figc=np.concatenate((figa,figb),axis=1)
# Save without a cmap, to preserve the ones you saved earlier
plt.imsave('figc.png',figc,cmap=None)
cmap
图b,jet
cmap
图c