2017-12-27 407 views
0

我在教自己的python数学动画,所以我甚至不知道我问的是否可以用matplotlib。我想要的是 (1)的轴开始小,然后展开。在此之后,我想要它 (2)顺序(一个接一个地)绘制两行,而不是同时(在同一时间),它正在做什么。
我的代码如下,除了我尝试扩展轴(最好我的尝试没有崩溃蟒蛇)。在python和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

我不知道如何将你的三个步骤连在一起,我不确定有没有一个优雅的方式。

不过,我尝试了创建与下面的代码为“成长”轴:

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

编辑

其实,我今天在思考这个问题,我有想到创建一个主函数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!

注:我不得不降低图形的大小以适合的imgur