18

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()

これが私がイメージを見る方法です

4

5 に答える 5

17

を使用してみてくださいpad_inches=0

plt.savefig( IMG_DIR + 'match.png',bbox_inches='tight', transparent="True", pad_inches=0)

ドキュメントから:

pad_inches: bbox_inches が 'tight' の場合の図の周りのパディングの量。

デフォルトだと思うpad_inches=0.1

于 2012-07-24T23:57:09.790 に答える
7

plt.tight_layout()前に追加するだけplt.savefig()!!

plt.figure(figsize=(16, 10))

# ... Doing Something ...

plt.tight_layout()
plt.savefig('wethers.png')
plt.show()
于 2020-11-01T13:40:24.233 に答える
3

以前のアプローチはすべてうまくいきませんでした。図の周りにパディングが残っていました。

次の行は、残っていた白または透明のパディングを正常に削除しました。

plt.axis('off')
ax = plt.gca()
ax.xaxis.set_major_locator(matplotlib.ticker.NullLocator())
ax.yaxis.set_major_locator(matplotlib.ticker.NullLocator())
plt.savefig(IMG_DIR + 'match.png', pad_inches=0, bbox_inches='tight', transparent=True)
于 2018-05-08T11:07:03.687 に答える