そのうちまとめるけれど、とりえず、これまでどんなのを使っているか、 並べておく。
import matplotlib.pyplot as plt |
plt.plot(x,y) plot.show() |
plt.plot(t,sol, label='x0='+'{:6.1f}'.format(x0))
|
plt.legend(loc='upper left') |
plt.plot(t,x[:,0],'b', label='S') |
plt.title('Malthus: dx/dt=ax, x(0)=x0; a='+str(a))
|
plt.xlim(0.0, 1.0) plt.ylim(-1.0, 2.0) |
plot.ion() line,=plt.plot(x,u) ... # u の内容を更新 line.set_ydata(u) plt.pause(0.001) |
plt.savefig('myfig.png')
|
| savefig() は plt.show() の前にする |
plt.savefig('なんとか.png')
plt.show()
|
Saving a figure after invoking pyplot.show() results in an empty file がとても分かりやすい。
少し面倒だけれど、 絵を描き出す前に fig, ax = plt.subplots() として、 fig に figure を覚えさせておいて、 plt.savefig() でなく fig.savefig() を実行するのが、 一つの堅実なやり方。確かにこういうコードはよく目にする。 次のような感じ (上記サイトに載っているコード例)。
| これがお勧めのやり方か |
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-1, 1, 100)
y = x**2
fig, ax = plt.subplots()
ax.plot(x, y)
fig.savefig('fig1.pdf')
plt.show()
fig.savefig('fig2.pdf')
|
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-1, 1, 100)
y = x**2
plt.plot(x, y)
fig = plt.gcf()
fig.savefig('fig1.pdf')
plt.show()
fig.savefig('fig2.pdf')
|
fig = plt.figure()
...
fig.savefig('fig2-1.png')
|