我的动画没有正确绘图

时间:2018-04-08 01:31:59

标签: python python-3.x matplotlib scipy jupyter-notebook

  

问题

我需要动画流畅,但它是逐帧绘制的。代码在Jupyter Notebook中运行。

  

以下是图书馆

struct user {
  char id[10];
  char name[25];
  char group;
  float score;
};

struct user list[25];
int registered = 0;

void createNewUser() {
  struct user *userPtr, newUser;
  userPtr = &newUser;

  printf("\nId: ");
  scanf("%10s", &(*userPtr).id);
  printf("\nName: ");
  scanf("%25s", &(*userPtr.name);
  printf("\nGroup: ");
  scanf("%c", &(*userPtr).group);
  printf("\nScore: ");
  scanf("%f", &(*userPtr).score);

  insert(newUser);

  printf("%10s\n", list[0].id);
  printf("%25s\n", list[0].name);
  printf("%c\n", list[0].group);
  printf("%.1f\n", list[0].score);
}

void insert(struct user newUser) {
  if (registered < 25){
    list[registered] = newUser;
    registered += 1;
  }
}
  

创建函数以进行卷积

import numpy as np
from matplotlib import pyplot as plt
from scipy import signal as sp
  

这是绘图

t_ini=0
t_final = 11
dt=0.1
t = np.arange(t_ini,t_final,dt)
expo = np.exp(-t)*np.piecewise(t,t>=0,[1,0])
t1 = np.arange(0,10,0.1)
s = np.sin(t1)
conv_=sp.convolve(s,expo,'full')
n_conv=np.arange(min(t1)+min(t),max(t1)+max(t)+0.1,0.1)
y = [0] * len(conv_)
t2 = [0] * len(n_conv)

1 个答案:

答案 0 :(得分:0)

matplotlib提供了实例ArtistAnimation,允许预先计算的图形的无缝动画。我刚给你的代码添加了几行代码。我改变的只是使用enumerate来改进你的代码

import numpy as np
from matplotlib import pyplot as plt
from scipy import signal as sp
import matplotlib.animation as anim

t_ini=0
t_final = 11
dt=0.1
t = np.arange(t_ini,t_final,dt)
expo = np.exp(-t)*np.piecewise(t,t>=0,[1,0])
t1 = np.arange(0,10,0.1)
s = np.sin(t1)
conv_=sp.convolve(s,expo,'full')
n_conv=np.arange(min(t1)+min(t),max(t1)+max(t)+0.1,0.1)
y = [0] * len(conv_)
t2 = [0] * len(n_conv)

#prepare figure for display
fig = plt.figure()
ax = plt.axes()

#create list to collect graphs for animation
img = []
for i, x in enumerate(n_conv):

    y[i] = conv_[i]
    t2[i] = n_conv[i]
    #append new graphs to  list
    newpic, = ax.plot(t2, y, c= "blue")
    img.append([newpic])

#animate the list of precalculated graphs
ani = anim.ArtistAnimation(fig, img, interval = 50)
plt.show()

输出:

enter image description here