matplotlibを使用して20以上の異なる画像を作成している状況があります。これは何度も行われます。20枚の画像のそれぞれは、背景に同じ輪郭のセットを持っています。countourf()
処理時間を短縮するには、あるAxes
インスタンスから別のインスタンスに結果をコピーできると便利です。これを行うために、私はこれを試しました:
#!/bin/env python
import os
import numpy as np
from matplotlib import pyplot as plt
def copycontours():
#Create figures
fig1 = plt.figure()
fig2 = plt.figure()
fig3 = plt.figure()
#Create axes
ax1 = fig1.add_axes((0.05,0.05,0.90,0.90))
ax2 = fig2.add_axes((0.05,0.05,0.90,0.90))
ax3 = fig3.add_axes((0.05,0.05,0.90,0.90))
#Create random data
data = np.random.normal(25, size=(25,25))
#Add contours to first axes instance and save image
contours = ax1.contourf(data)
fig1.savefig('test.png')
#Add contours to second axes instance from first axes instance
for collection in ax1.collections:
ax2.add_collection(collection)
fig2.savefig('test2.png')
#Add contours to third axes instance from
for collection in contours.collections:
ax3.add_collection(collection)
fig3.savefig('test3.png')
os.system('display test.png &')
os.system('display test2.png &')
os.system('display test3.png &')
if __name__ == '__main__':
copycontours()
最初の図(test.png)は正しく見えます。軸の範囲は0〜25で、ドメイン全体が塗りつぶされます。
他の2つ(test2.png、test3.png)の出力は異なります。それらの軸の範囲は0から1で、等高線領域は0.0から約7.9の領域のみを塗りつぶします。
を介して軸制限をリセットするax2.set_xlim(0,25)
とax2.set_xlim(0,25)
、軸範囲が変更されますが、より大きな問題は修正されません。
この問題を修正する方法や、結果を別の方法で再利用するための別の方法について誰かが考えていますcontourf()
か?