반응형

for 문에 Matplotlib 그래프를 저장하는 예제입니다. 

2021. 11. 29 - 최초작성



x좌표를 바꾸어가면서 그린 그래프 20개를 저장하는 예제입니니다.

 



다음과 같은 경고 메시지가 발생했었는데 plt.close(fig)를 추가하여 사라졌습니다. 

 

/home/webnautes/work/a1.py:7: RuntimeWarning: More than 20 figures have been opened. Figures created through the pyplot interface (`matplotlib.pyplot.figure`) are retained until explicitly closed and may consume too much memory. (To control this warning, see the rcParam `figure.max_open_warning`).

fig, ax = plt.subplots()



import matplotlib.pyplot as plt
import numpy as np


for i in range(30):

    fig, ax = plt.subplots()
    offset = i*360
    x = np.linspace(offset+0, offset+360, 360)
    x = np.deg2rad(x)

    ax.plot(x, np.sin(x))

    plt.xlabel('Angle [rad]')
    plt.ylabel('sin(x)')
    plt.axis('tight')
    plt.savefig('img_{}'.format(i))
    # plt.show()
    plt.close(fig)





참고

 

https://stackoverflow.com/questions/21884271/warning-about-too-many-open-figures

 

반응형

문제 발생시 지나치지 마시고 댓글 남겨주시면 가능한 빨리 답장드립니다.

도움이 되셨다면 토스아이디로 후원해주세요.
https://toss.me/momo2024


제가 쓴 책도 한번 검토해보세요 ^^

+ Recent posts