2017-12-27 9 views
0

나는 파이썬 수학 애니메이션을 가르치기 때문에 matplotlib로 가능한지 물어 보는지도 모르겠다. 내가 원하는 것은 (1) 축이 작게 시작한 다음 확장합니다. 이 후, 나는 그것을 수행하고있는 동시에 동시에 (동시에) 두 줄을 순차적으로 (하나씩) 차례대로 플롯 ( )하고 싶습니다.
내 코드가 축을 확장하려고 시도한 것을 제외하고는 (내 시도가 파이썬을 크래시하지 않았습니다.)파이썬과 matplotlib에서 축 크기와 2 개의 순차 애니메이션 애니메이션하기

import time 
import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib.animation as animation 
#from matplotlib.animation import FuncAnimation 


maxsize = 8 
size = 1 
fig = plt.figure(figsize=(maxsize, maxsize)) 
# fig will start at "size" if I ever figure out the animation 
xmin = -10 
xmax = 10 
ymin = -10 
ymax = 10 
ax = plt.axes(xlim=(xmin, xmax), ylim=(ymin, ymax)) 
plt.axhline(linewidth=3, color='black') 
plt.axvline(linewidth=3, color='black') 
line, = ax.plot([], [], lw=2) 


def init1(): 
    line.set_data([], []) 
    return line, 

def animate1(i): 
    x1 = np.linspace(-10, i/5 - 10, 1000) 
    y1 = -1*x1 
    line.set_data(x1, y1) 
    return line, 

def init2(): 
    line.set_data([], []) 
    return line, 

def animate2(j): 
    x2 = np.linspace(0, j/10, 1000) 
    y2 = 2*x2 
    line.set_data(x2, y2) 
    return line, 

plt.grid() 
plt.xticks(np.arange(xmin, xmax+1, 1.0)) 
plt.yticks(np.arange(ymin, ymax+1, 1.0)) 

anim1 = animation.FuncAnimation(fig, animate1, init_func=init1, 
    frames=100, interval=20, blit=True) 
plt.plot() 
plt.show() 
time.sleep(1) 
anim2 = animation.FuncAnimation(fig, animate2, init_func=init2, 
    frames=100, interval=20, blit=True) 
plt.plot() 
plt.show() 

그래서 내 질문이 추측 : 내가하기 matplotlib 가능하고 싶은 무엇인가? 하나? 다른? 양자 모두? 그렇다면 어떻게해야합니까?

+0

의 크기 제한 아래에 맞게 그림의 크기를 줄일 수 있었다 당신이 원하는 것이 무엇인지는 분명하지 않습니다. '(1) 축이 작고 나서 확장되기 시작합니다'라는 것이 몇 가지 종류의 확대/축소 효과입니까? '(2) 두 줄을 동시에 그리기보다는 순차적으로 그려라 .' 같은 줄에 한 줄을 그리고 다른 줄을 그려야겠습니까? 또는 첫 번째 줄에 하나의 줄을 표시하고 두 번째 줄에 다른 줄을 표시하고 싶습니까? –

+0

(1) 나는 축을 작게 시작하고, 1을 "1"씩, 8 "을 8"로 늘리자. (2) 줄거리에 한 줄을 그려서 멈추고 동일한 줄거리에 두 번째 줄을 그립니다. – SaintCad

답변

0

3 단계를 함께 묶을 수있는 방법을 모르겠다. 우아한 방법이 있는지 모르겠다.

그러나, 나는 "성장"는 아래의 코드로 축 만드는 실험 :

fig_size_w = 9 # inches 
fig_size_h = 9 # inches 
ax_bottom_x = 0.1 # fig fraction 
ax_bottom_y = 0.1 # fig fraction 
ax_init_x = 0.1 # fig fraction 
ax_init_y = 0.1 # fig fraction 
ax_final_x = 0.9 # fig fraction 
ax_final_y = 0.9 # fig fraction 
grow_steps = 100 # number of steps in the growing process 
xmin = -10 
xmax = 10 
ymin = -10 
ymax = 10 

grow_step_x = (ax_final_x - ax_init_x)/grow_steps 
grow_step_y = (ax_final_y - ax_init_y)/grow_steps 

fig = plt.figure(figsize=(fig_size_w, fig_size_h)) 
ax = fig.add_axes([ax_bottom_x, ax_bottom_y, 0, 0]) 

def init(): 
    ax.axhline(linewidth=3, color='black') 
    ax.axvline(linewidth=3, color='black') 
    ax.grid() 
    ax.set_xticks(np.arange(xmin, xmax+1, 1.0)) 
    ax.set_yticks(np.arange(ymin, ymax+1, 1.0)) 

def animate(i): 
    ax.set_position([ax_bottom_x, ax_bottom_y, (i+1)*grow_step_x, (i+1)*grow_step_y]) 


anim = matplotlib.animation.FuncAnimation(fig, animate, frames=grow_steps, init_func=init, interval=10) 

enter image description here

EDIT 사실

, 오늘이 문제에 대해 생각하고, 내가했다 프레임 목록을 담고있는 사전을 매개 변수로 받아들이는 마스터 animate() 함수를 만드는 생각과 각 프레임에 대해 실행할 함수 포인터. 코드는 많이 향상하지만, 개념의 증거 일 수있다 :

fig_size_w = 9 # inches 
fig_size_h = 9 # inches 
ax_bottom_x = 0.1 # fig fraction 
ax_bottom_y = 0.1 # fig fraction 
ax_init_x = 0.1 # fig fraction 
ax_init_y = 0.1 # fig fraction 
ax_final_x = 0.9 # fig fraction 
ax_final_y = 0.9 # fig fraction 
grow_steps = 100 # number of steps in the growing process 
xmin = -10 
xmax = 10 
ymin = -10 
ymax = 10 

grow_step_x = (ax_final_x - ax_init_x)/grow_steps 
grow_step_y = (ax_final_y - ax_init_y)/grow_steps 

fig = plt.figure(figsize=(fig_size_w, fig_size_h)) 
ax = fig.add_axes([ax_bottom_x, ax_bottom_y, 0, 0]) 
line, = ax.plot([],[], lw=2) 

def init(): 
    ax.axhline(linewidth=3, color='black') 
    ax.axvline(linewidth=3, color='black') 
    ax.grid() 
    ax.set_xticks(np.arange(xmin, xmax+1, 1.0)) 
    ax.set_yticks(np.arange(ymin, ymax+1, 1.0)) 

def animate(i, func_dict): 
    a = [] 
    for max_i,func in func_dict.items(): 
     if i < max_i: 
      a = func(i) 
      break 
    return a 

def func_grow(i): 
    ax.set_position([ax_bottom_x, ax_bottom_y, (i+1)*grow_step_x, (i+1)*grow_step_y]) 
    return [] 

def func_pause(i): 
    pass 
    return [] 

def func_line1(i): 
    i-=(grow_steps+100) # need to remove offset value in i for it to start at 0 
    x1 = np.linspace(-10, i/5 - 10, 1000) 
    y1 = -1*x1 
    line.set_data(x1, y1) 
    return line, 

def func_line2(j): 
    j-=(grow_steps+300) # need to remove offset value in j for it to start at 0 
    x2 = np.linspace(0, j/10, 1000) 
    y2 = 2*x2 
    line.set_data(x2, y2) 
    return line, 


anim = matplotlib.animation.FuncAnimation(fig, animate, frames=grow_steps+400, init_func=init, interval=10, 
              fargs=({grow_steps: func_grow, 
                grow_steps+100: func_pause, 
                grow_steps+200: func_line1, 
                grow_steps+300: func_pause, 
                grow_steps+400: func_line2},)) 

3 animations in one!

NB를 : 나는 imgur

관련 문제