【问题标题】:Animating axes size and 2 sequential animations in python and matplotlib在 python 和 matplotlib 中动画轴大小和 2 个顺序动画
【发布时间】:2017-12-27 18:24:07
【问题描述】:

我正在自学 python 数学动画,所以我什至不知道我所问的是否可以使用 matplotlib。我想要的是 (1) 轴从小开始,然后扩大。在此之后,我希望它 (2) 按顺序(一个接一个)而不是同时(同时)绘制两条线,这就是它正在做的事情。
我的代码在下面,除了我尝试扩展轴(充其量我的尝试没有使 python 崩溃)。

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 中做的事情是可能的吗?一?另一个?两个都? 如果是这样,我该怎么做。

【问题讨论】:

  • 我不太清楚你想要什么;让我们试着澄清一下:(1)for the axes to start off small then expand 这是数字轴上的某种缩放效果吗? (2) plot two lines sequentially rather than concurrently 您想在同一个图上绘制一条线,然后是另一条线(保留第一条线)?或者你想先展示一个情节,然后再展示第二个情节?
  • (1) 我希望轴从小开始,假设 1" x 1" 然后增长到 8" x 8"。 (2) 我想在一个图上画一条线,暂停,然后在同一个图上画第二条线。

标签: python animation matplotlib


【解决方案1】:

我不知道您如何将三个步骤链接在一起,我不确定是否有一种优雅的方式。

但是,我尝试使用以下代码创建“增长”轴:

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)

编辑

其实我今天也在思考这个问题,我想创建一个主控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},))

注意:我不得不减小图形的大小以适应 imgur 的大小限制

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多