Python matplotlib图形到无白边框的numpy数组

Python matplotlib图形到无白边框的numpy数组,python,numpy,matplotlib,Python,Numpy,Matplotlib,我试图找到一种方法,将matplotlib图形转换为无任何空格或边框的numpy数组 我发现如果我使用下面的代码,我会得到一些非常接近的结果,但是numpy数组“data”仍然会有一些白色边框。白色边框(在缩放窗口时白色会发生变化)保存为numpy数组的一部分,但我只需要内容 有没有办法摆脱这个白色区域 fig = plt.figure(frameon=False) ax = plt.Axes(fig, [0., 0., 1., 1.]) ax.set_axis_off() ax.set_yli

我试图找到一种方法,将matplotlib图形转换为无任何空格或边框的numpy数组

我发现如果我使用下面的代码,我会得到一些非常接近的结果,但是numpy数组“data”仍然会有一些白色边框。白色边框(在缩放窗口时白色会发生变化)保存为numpy数组的一部分,但我只需要内容

有没有办法摆脱这个白色区域

fig = plt.figure(frameon=False)
ax = plt.Axes(fig, [0., 0., 1., 1.])
ax.set_axis_off()
ax.set_ylim(height, 0)
ax.set_xlim(0, width)
ax.axis('off')
fig.add_axes(ax)

ax.imshow(myimage) # Plus lots of other things

plt.show() # fig.canvas.draw() also works the same
data = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8, sep='')
data = data.reshape(fig.canvas.get_width_height()[::-1] + (3,))
plt.close()


之前也有人问过类似的问题,但作者自己对PIL的研究并不符合我的需要。

我想
子地块调整
是关键所在。似乎可以使用一个玩具示例,但如果您能够提供最少的工作代码来查看它在您的用例中是否有效,那就太好了

import numpy as np
import matplotlib as mpl
# mpl.use('Agg')
import matplotlib.pyplot as plt

img = np.ones((100, 200))
img[25:75, 50:150] = 0

fig = plt.figure()
ax = fig.gca()

ax.imshow(img)
ax.axis('tight')

plt.subplots_adjust(0,0,1,1,0,0)

plt.show()

data = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8, sep='')
w, h = fig.canvas.get_width_height()
data = data.reshape((h, w, 3))

plt.close()

您可以尝试使用
plt.figure()
figsize
参数指定与图像具有相同纵横比的图形大小。但这似乎是一个间接的方法来实现你想要的,我不知道它是否会完美地消除利润。对我来说是有效的。谢谢。子批次调整(0,0,1,1,0,0)这是一个人需要做的