matplotlib.animation에서 그래프 애니메이션
14600 단어 파이썬matplotlibmatplotlib.animation
matplotlib.animation이란?
값이나 디자인을 바꾼 그래프를 연속해서 그리는 것으로 애니메이션 시키는 matplotlib의 기능입니다. gif 이미지나 동영상 파일로 내보내거나 html+JavaScript로 내보낼 수도 있으므로 2차 이용도 하기 쉽다고 생각합니다.
1. 설치
matplotlib를 설치하기만 하면 됩니다.
terminalpip install matplotlib
2. 두 종류
두 가지 클래스 중에서 선택할 수 있습니다.
matplotlib.animation.ArtistAnimation
그리기 정보의 list를 만들어 그리는 클래스입니다.
matplotlib.animation.FuncAnimation
렌더링 정보를 갱신하는 함수를 건네주고 순차 실행으로 렌더링 하는 클래스입니다
난수를 사용하여 매번 다른 그래프를 그리는 것과 같은 사용법을 할 수 있습니다
3. 사용법
3-1. terminal 실행에 사용
이 코드를 빌려 보겠습니다.
htps : // 이 m / 유바이 s / ms / c95 바 9 f1b23 d33f에서 2
1. ArtistAnimation의 경우
plt.plot()등으로 만든 묘화분의 오브젝트를 list로 해 ArtistAnimation에 던져 주면 애니메이션 묘화를 해 줍니다. ArtistAnimation에서는 먼저 그리기 정보를 만들어 놓고 나서 버리므로 반복 패턴은 동일합니다.
artist_animation.pyimport numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig = plt.figure()
ims = []
for i in range(10):
rand = np.random.randn(100)
im = plt.plot(rand)
ims.append(im)
ani = animation.ArtistAnimation(fig, ims, interval=100)
plt.show()
terminal에서 python artist_animation.py를 실행하면 이러한 느낌이 표시 될 것입니다.
![](https://s1.md5.ltd/image/944e6701087474852044d0a3ac50d745.gif)
2. FuncAnimation의 경우
list로 던져주는 것은 같지만 그리는 함수
정의하고 전달하면 매번 다른 난수를 표시 할 수 있습니다
func_animation.pyimport numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig = plt.figure()
def plot(data):
plt.cla()
rand = np.random.randn(100)
im = plt.plot(rand)
ani = animation.FuncAnimation(fig, plot, interval=100)
plt.show()
terminal에서 python func_animation.py를 실행하면 이러한 느낌이 표시 될 것입니다. 아래에 붙이고 있는 것은 나중에 설명하는 방식으로 내보낸 gif 이미지이므로 같은 패턴의 반복이 되고 있습니다만, python이 표시해 주는 window의 분은 제대로 난수를 재작성해 매회 다른 패턴을 표시해 줄거야.
![](https://s1.md5.ltd/image/891307a3dc5ba27a551ed75695d2c9f2.gif)
덧붙여 상기에서는 plt.cla() 하고 있으므로 축이 매회 재작성되고 있습니다만, 변하지 않게 쓸 수도 있습니다.
3-2. gif 이미지로 저장하기
animation 객체의 save 메소드를 사용하면 저장할 수 있습니다.
func_animation.py에 한 줄을 더해ani.save('func_animation.gif', writer='pillow')
writer 클래스로서는 Pillow, FFMpeg, ImageMagick등을 선택할 수 있습니다만, pip로 간단하게 인스톨 할 수 있는 Pillow가 간편하다고 생각합니다.
3-3. jupyter notebook에 표시
위의 plt.show()로 표시하는 방법은 jupyter notebook에서는 할 수 없는 것 같습니다만, gif 이미지를 내보낸 후 표시하거나 Javascript html로 변환하여 IPython.display.HTML에 표시하도록 하는 것으로 표시 수 있습니다. 또 h264 encoder가 있는 환경이라면 동영상으로 변환해 IPython.display.HTML로 표시하는 방법도 사용할 수 있는 것 같습니다.
htps : // m / f 병아리 / ms / 0 음 94b0, 6cd5c76d67a
1. gif 이미지를 내보내고 표시
jupyter_notebookimport numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from IPython.display import Image
fig = plt.figure()
ims = []
for i in range(10):
rand = np.random.randn(100)
im = plt.plot(rand)
ims.append(im)
ani = animation.ArtistAnimation(fig, ims, interval=100)
ani.save('../artist_animation.gif', writer='pillow')
plt.close()
Image('../artist_animation.gif')
2. Javascript html로 변환하여 표시
to_jshtml() 메소드를 사용하면 html + Javascript로 변환합니다.
jupyter_notebookimport numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from IPython.display import HTML
fig = plt.figure()
ims = []
for i in range(10):
rand = np.random.randn(100)
im = plt.plot(rand)
ims.append(im)
ani = animation.ArtistAnimation(fig, ims, interval=100)
plt.close()
HTML(ani.to_jshtml())
FuncAnimation으로 쓰고 싶다면 다음과 같이하십시오.
jupyter_notebookimport numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from IPython.display import HTML
fig = plt.figure()
N = 100
rand = np.random.randn(N)
im, = plt.plot(rand)
def update(frame):
x = np.arange(N)
y = np.random.randn(N)
im.set_data(x, y)
ani = animation.FuncAnimation(fig, update, interval=100)
plt.close()
HTML(ani.to_jshtml())
4. FuncAnimation과 ArtistAnimation의 구분
FuncAnimation으로 써도 gif 이미지 내보내기나 Javascript로 내보내는 것은 한 번 돌려 생성한 곳까지의 정보로 내보내는 것이므로 난수를 반복해서 생성하는 것은 할 수 없습니다. 실행 시간은 FuncAnimation으로 쓰는 것이 걸리므로 jupyter notebook에서 사용하는 경우는 FuncAnimation으로 쓰는 의미는별로 없다고 생각합니다.
Reference
이 문제에 관하여(matplotlib.animation에서 그래프 애니메이션), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/studio_haneya/items/891c4ea6a7326919e381
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
matplotlib를 설치하기만 하면 됩니다.
terminal
pip install matplotlib
2. 두 종류
두 가지 클래스 중에서 선택할 수 있습니다.
matplotlib.animation.ArtistAnimation
그리기 정보의 list를 만들어 그리는 클래스입니다.
matplotlib.animation.FuncAnimation
렌더링 정보를 갱신하는 함수를 건네주고 순차 실행으로 렌더링 하는 클래스입니다
난수를 사용하여 매번 다른 그래프를 그리는 것과 같은 사용법을 할 수 있습니다
3. 사용법
3-1. terminal 실행에 사용
이 코드를 빌려 보겠습니다.
htps : // 이 m / 유바이 s / ms / c95 바 9 f1b23 d33f에서 2
1. ArtistAnimation의 경우
plt.plot()등으로 만든 묘화분의 오브젝트를 list로 해 ArtistAnimation에 던져 주면 애니메이션 묘화를 해 줍니다. ArtistAnimation에서는 먼저 그리기 정보를 만들어 놓고 나서 버리므로 반복 패턴은 동일합니다.
artist_animation.pyimport numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig = plt.figure()
ims = []
for i in range(10):
rand = np.random.randn(100)
im = plt.plot(rand)
ims.append(im)
ani = animation.ArtistAnimation(fig, ims, interval=100)
plt.show()
terminal에서 python artist_animation.py를 실행하면 이러한 느낌이 표시 될 것입니다.
![](https://s1.md5.ltd/image/944e6701087474852044d0a3ac50d745.gif)
2. FuncAnimation의 경우
list로 던져주는 것은 같지만 그리는 함수
정의하고 전달하면 매번 다른 난수를 표시 할 수 있습니다
func_animation.pyimport numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig = plt.figure()
def plot(data):
plt.cla()
rand = np.random.randn(100)
im = plt.plot(rand)
ani = animation.FuncAnimation(fig, plot, interval=100)
plt.show()
terminal에서 python func_animation.py를 실행하면 이러한 느낌이 표시 될 것입니다. 아래에 붙이고 있는 것은 나중에 설명하는 방식으로 내보낸 gif 이미지이므로 같은 패턴의 반복이 되고 있습니다만, python이 표시해 주는 window의 분은 제대로 난수를 재작성해 매회 다른 패턴을 표시해 줄거야.
![](https://s1.md5.ltd/image/891307a3dc5ba27a551ed75695d2c9f2.gif)
덧붙여 상기에서는 plt.cla() 하고 있으므로 축이 매회 재작성되고 있습니다만, 변하지 않게 쓸 수도 있습니다.
3-2. gif 이미지로 저장하기
animation 객체의 save 메소드를 사용하면 저장할 수 있습니다.
func_animation.py에 한 줄을 더해ani.save('func_animation.gif', writer='pillow')
writer 클래스로서는 Pillow, FFMpeg, ImageMagick등을 선택할 수 있습니다만, pip로 간단하게 인스톨 할 수 있는 Pillow가 간편하다고 생각합니다.
3-3. jupyter notebook에 표시
위의 plt.show()로 표시하는 방법은 jupyter notebook에서는 할 수 없는 것 같습니다만, gif 이미지를 내보낸 후 표시하거나 Javascript html로 변환하여 IPython.display.HTML에 표시하도록 하는 것으로 표시 수 있습니다. 또 h264 encoder가 있는 환경이라면 동영상으로 변환해 IPython.display.HTML로 표시하는 방법도 사용할 수 있는 것 같습니다.
htps : // m / f 병아리 / ms / 0 음 94b0, 6cd5c76d67a
1. gif 이미지를 내보내고 표시
jupyter_notebookimport numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from IPython.display import Image
fig = plt.figure()
ims = []
for i in range(10):
rand = np.random.randn(100)
im = plt.plot(rand)
ims.append(im)
ani = animation.ArtistAnimation(fig, ims, interval=100)
ani.save('../artist_animation.gif', writer='pillow')
plt.close()
Image('../artist_animation.gif')
2. Javascript html로 변환하여 표시
to_jshtml() 메소드를 사용하면 html + Javascript로 변환합니다.
jupyter_notebookimport numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from IPython.display import HTML
fig = plt.figure()
ims = []
for i in range(10):
rand = np.random.randn(100)
im = plt.plot(rand)
ims.append(im)
ani = animation.ArtistAnimation(fig, ims, interval=100)
plt.close()
HTML(ani.to_jshtml())
FuncAnimation으로 쓰고 싶다면 다음과 같이하십시오.
jupyter_notebookimport numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from IPython.display import HTML
fig = plt.figure()
N = 100
rand = np.random.randn(N)
im, = plt.plot(rand)
def update(frame):
x = np.arange(N)
y = np.random.randn(N)
im.set_data(x, y)
ani = animation.FuncAnimation(fig, update, interval=100)
plt.close()
HTML(ani.to_jshtml())
4. FuncAnimation과 ArtistAnimation의 구분
FuncAnimation으로 써도 gif 이미지 내보내기나 Javascript로 내보내는 것은 한 번 돌려 생성한 곳까지의 정보로 내보내는 것이므로 난수를 반복해서 생성하는 것은 할 수 없습니다. 실행 시간은 FuncAnimation으로 쓰는 것이 걸리므로 jupyter notebook에서 사용하는 경우는 FuncAnimation으로 쓰는 의미는별로 없다고 생각합니다.
Reference
이 문제에 관하여(matplotlib.animation에서 그래프 애니메이션), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/studio_haneya/items/891c4ea6a7326919e381
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
3-1. terminal 실행에 사용
이 코드를 빌려 보겠습니다.
htps : // 이 m / 유바이 s / ms / c95 바 9 f1b23 d33f에서 2
1. ArtistAnimation의 경우
plt.plot()등으로 만든 묘화분의 오브젝트를 list로 해 ArtistAnimation에 던져 주면 애니메이션 묘화를 해 줍니다. ArtistAnimation에서는 먼저 그리기 정보를 만들어 놓고 나서 버리므로 반복 패턴은 동일합니다.
artist_animation.py
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig = plt.figure()
ims = []
for i in range(10):
rand = np.random.randn(100)
im = plt.plot(rand)
ims.append(im)
ani = animation.ArtistAnimation(fig, ims, interval=100)
plt.show()
terminal에서 python artist_animation.py를 실행하면 이러한 느낌이 표시 될 것입니다.
![](https://s1.md5.ltd/image/944e6701087474852044d0a3ac50d745.gif)
2. FuncAnimation의 경우
list로 던져주는 것은 같지만 그리는 함수
정의하고 전달하면 매번 다른 난수를 표시 할 수 있습니다
func_animation.py
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig = plt.figure()
def plot(data):
plt.cla()
rand = np.random.randn(100)
im = plt.plot(rand)
ani = animation.FuncAnimation(fig, plot, interval=100)
plt.show()
terminal에서 python func_animation.py를 실행하면 이러한 느낌이 표시 될 것입니다. 아래에 붙이고 있는 것은 나중에 설명하는 방식으로 내보낸 gif 이미지이므로 같은 패턴의 반복이 되고 있습니다만, python이 표시해 주는 window의 분은 제대로 난수를 재작성해 매회 다른 패턴을 표시해 줄거야.
![](https://s1.md5.ltd/image/891307a3dc5ba27a551ed75695d2c9f2.gif)
덧붙여 상기에서는 plt.cla() 하고 있으므로 축이 매회 재작성되고 있습니다만, 변하지 않게 쓸 수도 있습니다.
3-2. gif 이미지로 저장하기
animation 객체의 save 메소드를 사용하면 저장할 수 있습니다.
func_animation.py에 한 줄을 더해
ani.save('func_animation.gif', writer='pillow')
writer 클래스로서는 Pillow, FFMpeg, ImageMagick등을 선택할 수 있습니다만, pip로 간단하게 인스톨 할 수 있는 Pillow가 간편하다고 생각합니다.
3-3. jupyter notebook에 표시
위의 plt.show()로 표시하는 방법은 jupyter notebook에서는 할 수 없는 것 같습니다만, gif 이미지를 내보낸 후 표시하거나 Javascript html로 변환하여 IPython.display.HTML에 표시하도록 하는 것으로 표시 수 있습니다. 또 h264 encoder가 있는 환경이라면 동영상으로 변환해 IPython.display.HTML로 표시하는 방법도 사용할 수 있는 것 같습니다.
htps : // m / f 병아리 / ms / 0 음 94b0, 6cd5c76d67a
1. gif 이미지를 내보내고 표시
jupyter_notebook
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from IPython.display import Image
fig = plt.figure()
ims = []
for i in range(10):
rand = np.random.randn(100)
im = plt.plot(rand)
ims.append(im)
ani = animation.ArtistAnimation(fig, ims, interval=100)
ani.save('../artist_animation.gif', writer='pillow')
plt.close()
Image('../artist_animation.gif')
2. Javascript html로 변환하여 표시
to_jshtml() 메소드를 사용하면 html + Javascript로 변환합니다.
jupyter_notebook
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from IPython.display import HTML
fig = plt.figure()
ims = []
for i in range(10):
rand = np.random.randn(100)
im = plt.plot(rand)
ims.append(im)
ani = animation.ArtistAnimation(fig, ims, interval=100)
plt.close()
HTML(ani.to_jshtml())
FuncAnimation으로 쓰고 싶다면 다음과 같이하십시오.
jupyter_notebook
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from IPython.display import HTML
fig = plt.figure()
N = 100
rand = np.random.randn(N)
im, = plt.plot(rand)
def update(frame):
x = np.arange(N)
y = np.random.randn(N)
im.set_data(x, y)
ani = animation.FuncAnimation(fig, update, interval=100)
plt.close()
HTML(ani.to_jshtml())
4. FuncAnimation과 ArtistAnimation의 구분
FuncAnimation으로 써도 gif 이미지 내보내기나 Javascript로 내보내는 것은 한 번 돌려 생성한 곳까지의 정보로 내보내는 것이므로 난수를 반복해서 생성하는 것은 할 수 없습니다. 실행 시간은 FuncAnimation으로 쓰는 것이 걸리므로 jupyter notebook에서 사용하는 경우는 FuncAnimation으로 쓰는 의미는별로 없다고 생각합니다.
Reference
이 문제에 관하여(matplotlib.animation에서 그래프 애니메이션), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/studio_haneya/items/891c4ea6a7326919e381
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
Reference
이 문제에 관하여(matplotlib.animation에서 그래프 애니메이션), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/studio_haneya/items/891c4ea6a7326919e381텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)