matplotlib savefig() 真っ白になる問題

スポンサーリンク

はじめに

matplotlib でsavefig()を使ってグラフを保存したところ
写真が真っ白になってしまった。これの解決策についてのメモ

環境

macOS Mojave
Python 3.6.6

真っ白になった時のコード

真っ白になった時のコードがこれ

from matplotlib import pyplot as plt

fig = plt.figure()
fig.add_subplot(111)
plt.show()
plt.savefig("image1.png")

真っ白 f:id:shangtian:20191117150129p:plain

変更後

plt.show()の前にplt.savefig()を持って来ることで解決した

from matplotlib import pyplot as plt

fig = plt.figure()
fig.add_subplot(111)
plt.savefig("image1.png")
plt.show()

f:id:shangtian:20191117151123p:plain

<参考文献>

python グラフ 保存 真っ白 - 好きな言葉は「物理で殴る」

python - matplotlibで作成したグラフを保存すると真っ白な画像になってしまう - スタック・オーバーフロー

python - matplotlibで作成したグラフを保存すると真っ白な画像になってしまう - スタック・オーバーフロー