'Python'에 해당되는 글 1건

  1. 2019.01.04 python에서 matplotlib를 이용하여 움직이는 선 그리기

파이썬, 3.6 버전 기준에서그림을 그려보려 한다.

그림은 수직선이 왼쪽에서 오른쪽으로 반복 이동함.

 

1) 그림을 그리기 위한 matplotlib 라이브러리를 다운로드하자.

* 윈도우 커멘드 창에서 : pip install matplotlib

* 다운로드 후 확인해보면, matplotlib-3.0.2 버전을 받음


2) 코드 작성을 한다.

코드는 다음 사이트 에서 좀 수정한 내용입니다. :https://stackoverrun.com/ko/q/12087716

코드 변수명등이 매끄럽지 않네요. 추후 정리 필요할 듯...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import matplotlib.pyplot as plt 
import numpy as np 
from matplotlib.animation import FuncAnimation 
 
list_var_points = (0123456789101112131415)
 
 
fig, ax = plt.subplots() 
xfixdata, yfixdata = 715 
xdata = 0
ydata = 0
 
# line width
plt.rcParams['lines.linewidth'= 20
# plot의 여백 삭제, 박스를 전체 크기로
plt.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=0, hspace=0)
ln, = plt.plot('ro-', animated=True)
 
def init(): 
    ax.set_xlim(015
    ax.set_ylim(015
    return ln, 
 
def update(frame): 
    ydata = list_var_points[frame] 
# (시작선x, 끝선x), (시작선y, 끝선y)
    ln.set_data([ydata,ydata], [yfixdata,xdata])    
    return ln,   
 
# ani = FuncAnimation(fig, update, interval=100, frames=range(len(list_var_points)), 
#      init_func=init, blit=True)
ani = FuncAnimation(fig, update, frames=range(len(list_var_points)), 
      init_func=init, blit=True) 
 
plt.show() 
 
cs



< 아래의 파란 선이 왼쪽에서... 오른쪽으로 반복 이동 함>



기타 참고 자료 : 

https://financedata.github.io/posts/faq_matplotlib_default_chart_size.html

https://matplotlib.org/users/customizing.html


추후 애니메이션 효과가 더 필요하다면

game 라이브러리를 사용하는 것이 더 좋지 않을까 함

참고 : http://programarcadegames.com/index.php?chapter=bitmapped_graphics_and_sound 





 


Posted by 고무함지
,