and*_*ali 11 python django matplotlib padding
我正在matplotlib中绘制一个图像,它不断给我一些填充.这是我尝试过的:
def field_plot():
    x = [i[0] for i in path]
    y = [i[1] for i in path]
    plt.clf()
    plt.axis([0, 560, 0, 820])
    im = plt.imread('field.jpg')
    field = plt.imshow(im)
    for i in range(len(r)):
        plt.plot(r[i][0],r[i][1],c=(rgb_number(speeds[i]),0,1-rgb_number(speeds[i])),linewidth=1)
    plt.axis('off')
    plt.savefig( IMG_DIR + 'match.png',bbox_inches='tight', transparent="True")
    plt.clf()

尝试使用pad_inches=0,即
plt.savefig( IMG_DIR + 'match.png',bbox_inches='tight', transparent="True", pad_inches=0)
从文档:
pad_inches:当bbox_inches为"紧"时,数字周围的填充量.
我认为默认是 pad_inches=0.1
小智 8
plt.tight_layout()前面加上就可以了plt.savefig()!!
plt.figure(figsize=(16, 10))
# ... Doing Something ...
plt.tight_layout()
plt.savefig('wethers.png')
plt.show()
小智 5
这对我有用。绘图后,使用 ax = plt.gca() 从 plt 获取 Axes 对象。然后设置 ax 对象的 xlim 和 ylim 以匹配图像宽度和图像高度。绘图时,Matplotlib 似乎会自动增加查看区域的 xlim 和 ylim。请注意,在设置 y_lim 时,您必须反转坐标顺序。
for i in range(len(r)):
  plt.plot(r[i][0],r[i][1],c=(rgb_number(speeds[i]),0,1-rgb_number(speeds[i])),linewidth=1)
plt.axis('off')
ax = plt.gca();
ax.set_xlim(0.0, width_of_im);
ax.set_ylim(height_of_im, 0.0);
plt.savefig( IMG_DIR + 'match.png',bbox_inches='tight', transparent="True")