我尝试了所有建议的注释,以删除matplotlib中已保存图像周围的空白,但这对我不起作用。实际上,我看不到图像周围的白色框,但是在加载图像时,矩阵中存在白色值,因此它确实存在。我很高兴有人能解决这个问题。这是我的代码:
import matplotlib.pyplot as plt
## -----------------------Initialize-----------------------
pixels = 600
my_dpi = 100
coord = [[-150,-200],[300,-200],[300,0],[150,200],[-150,200]]
# Set figure dimensions
fig = plt.figure(figsize=( pixels/my_dpi, pixels/my_dpi),facecolor='k', dpi=my_dpi)
plt.axes([0,0,1,1])
rectangle = plt.Rectangle((-300, -300), 600, 600, fc='k')
plt.gca().add_patch(rectangle)
polygon = plt.Polygon(coord,color='w')
plt.gca().add_patch(polygon)
plt.axis('off')
plt.axis([-300,300,-300,300])
plt.axis('scaled')
plt.savefig('figure/%d.jpg',bbox_inches="tight", pad_inches=0,dpi=my_dpi)