如何保存具有透明度的imshow图片堆栈?

时间:2019-04-04 23:49:32

标签: python matplotlib

我正在尝试创建三个图像的堆栈,并将结果另存为具有透明背景的eps。以下是我到目前为止尝试过的内容:

尝试1:

import numpy as np
from matplotlib import pyplot as plt

red = np.random.normal(size=(100,100))
green = np.random.normal(size=(100,100))
blue = np.random.normal(size=(100,100))

fig,ax = plt.subplots()
ax.imshow(red,   cmap="Reds_r",   extent=( 0., 100.,  0., 100.), zorder=2)
ax.imshow(green, cmap="Greens_r", extent=(20., 120., 15., 115.), zorder=1)
ax.imshow(blue,  cmap="Blues_r",  extent=(40., 140., 30., 130.), zorder=0)

ax.set_xlim(0, 150)
ax.set_ylim(0, 150)
ax.set_axis_off()

plt.savefig("frames.eps", format="eps", transparent=True)
plt.show()

尝试2:

import numpy as np
from matplotlib import pyplot as plt

red = np.random.normal(size=(100,100))
green = np.random.normal(size=(100,100))
blue = np.random.normal(size=(100,100))

colors = np.ones(shape=(150,150,4))
colors[:,:,3] = 0.
colors[30:130,40:140,:] = mpl.cm.Blues_r( mpl.colors.Normalize()(blue) )
colors[15:115,20:120,:] = mpl.cm.Greens_r( mpl.colors.Normalize()(green) )
colors[0:100,0:100,:] = mpl.cm.Reds_r( mpl.colors.Normalize()(red) )

fig,ax = plt.subplots(facecolor="None")
ax.imshow(colors)

ax.set_xlim(0, 150)
ax.set_ylim(0, 150)
ax.set_axis_off()

plt.savefig("frames.eps", format="eps", transparent=True)
plt.show()

当我查看已保存的图像时,图像的某些部分在背景中具有黑色,不应有黑色。关于如何删除它的任何想法?

enter image description here

0 个答案:

没有答案