import numpy as np
import sys
import matplotlib as mpl
import matplotlib.pyplot as plt

我使用以下代码保存图像
fig, ax = plt.subplots(frameon=False)
ax.axis                 ('off')
ax.imshow               (array[:,:,0,0,0])
fig.savefig             ("file.png", bbox_inches='tight')

但是,我得到的是
python - 在python中使用子图和imshow时删除白色边框(Matplotlib)-LMLPHP
而且显然边框仍然是白色的。
我如何摆脱它?

array.shape是:(256,256,1,1,3)

最佳答案

看我的例子,它可能对您有帮助:

import numpy as np
import matplotlib.pyplot as plt

def save_image(data, filename):
    sizes = np.shape(data)
    fig = plt.figure()
    fig.set_size_inches(1. * sizes[0] / sizes[1], 1, forward = False)
    ax = plt.Axes(fig, [0., 0., 1., 1.])
    ax.set_axis_off()
    fig.add_axes(ax)
    ax.imshow(data)
    plt.savefig(filename, dpi = sizes[0], cmap='hot')
    plt.close()

data = np.random.randint(0, 100, (256, 256))
save_image(data, '1.png')

python - 在python中使用子图和imshow时删除白色边框(Matplotlib)-LMLPHP

关于python - 在python中使用子图和imshow时删除白色边框(Matplotlib),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37809697/

10-12 22:26
查看更多