3

グラフの背景画像を変更したい。いくつかの調査の後、私はこの方法を見つけました:

img = imread("name.jpg")
plt.scatter(x,y,zorder=1)
plt.imshow(img,zorder=0)
plt.show()

それは正常に動作し、その中にグラフを含むウィンドウを作成します。ただし、グラフをファイルに保存する必要がある場合は機能しないことがわかりました。

私は次のようなものを持っています:

plt.clf()
axe_lim = int(max([abs(v) for v in x_values+y_values])*1.4)
plt.plot(x_values, y_values)
plt.gcf().set_size_inches(10,10,forward='True') 
plt.axhline(color='r')
plt.axvline(color='r')
plt.title(label.upper(), size=25)
plt.xlim((-axe_lim,axe_lim))
plt.ylim((-axe_lim,axe_lim))
plt.xlabel(units)
plt.ylabel(units)
plt.grid(True)
plt.tight_layout()
img = imread("name.jpg")
plt.imshow(img)
plt.savefig(plot_pict)

バックグラウンドの呼び出しはどこに置く必要がありますか? よくある問題ですか、それとも私が行った呼び出しがバックグラウンドの変更を上書きしていますか? 助けてくれてありがとう。

4

1 に答える 1

4

うーん...それはあなたのフィギュアの範囲に問題があるかもしれません. 次に例を示します。

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111)
# a plot
t = np.linspace(0,2*np.pi,1000)
ax.plot(t * np.sin(t), t * np.cos(t), 'w', linewidth=3)
ax.plot(t * np.sin(t), t * np.cos(t), 'k', linewidth=1)

# create a  background image
X = np.linspace(0, np.pi, 100)
img = np.sin(X[:,None] + X[None,:])

# show the background image
x0,x1 = ax.get_xlim()
y0,y1 = ax.get_ylim()
ax.imshow(img, extent=[x0, x1, y0, y1], aspect='auto')

fig.savefig('/tmp/test.png')

ポイントは、軸の範囲が設定された後に画像を描画することです。最初に描画すると、軸領域から離れた場所にスケーリングされる場合があります。また、 を使用aspect='auto'すると、画像が縦横比を変更しようとしていないことが保証されます。(当然、画像は領域全体を埋めるために引き伸ばされます。)zorder必要に応じて を設定することもできますが、この例では必要ありません。

ここに画像の説明を入力

于 2014-07-06T17:24:54.537 に答える