データをクランチして結果を多軸図に表示するプログラムがあります。レポート形式に生成しようとしているさまざまな図のセットがあります。メモリを節約するために、1 つの Figure インスタンスを作成し、各ループの最後でクリアしています。以下はフォームの例です。
import matplotlib.pyplot as plt
import numpy as np
def the_figure():
#I want a figure that is persistent and accessible
#So I make the figure an attribute of a function
the_figure.fig = plt.figure()
the_figure.axes = dict(
t_ax = plt.subplot2grid((6,2),(1,0)),
t_fit_ax = plt.subplot2grid((6,2),(1,1)),
o_ax = plt.subplot2grid((6,2),(2,0)),
o_fit_ax = plt.subplot2grid((6,2),(2,1)),
table = plt.subplot2grid((6,2),(3,0),
rowspan = 3, colspan = 2)
)
#A function which makes figures using the single figure function
def Disp(i=5):
try:
the_figure.fig
except:
the_figure()
pi = 3.141592653589793
axes = the_figure.axes
xs = np.linspace(-pi/2,pi/2)
for n in range(i):
for name,ax in axes.items():
ax.plot(xs,np.sin(xs*n))
the_figure.fig.savefig('test_folder\\bad'+str(n),transparent=True)
the_figure.fig.savefig('test_folder\\good'+str(n),transparent=False)
#Clear the axes for reuse, supposedly
for name,ax in axes.items():
ax.cla()
終了すると、transparent=True で保存されたフィギュアは、ループからの曲線と前のループからの曲線のオーバーレイを取得します。何が起こっているのかわかりません。