#python #python-3.x #python-2.7 #matplotlib #animation
Вопрос:
Я хочу, чтобы у меня был список таких пунктов, как
list = [[1.0, 1.0], ... ,[17.3, 39.4]]
и я хочу сделать анимацию в matplotlib, где мой объект в первом кадре будет находиться в первой точке, второй кадр-во второй точке, и так далее и тому подобное…
Как бы я это сделал?
Я просто хочу знать общую идею.. самый простой возможный код, потому что я новичок в этой библиотеке python.
Комментарии:
Ответ №1:
Вот простой пример:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from IPython.display import HTML
# list of points
lst = [
[1.0, 1.0], [1.0, 2.0], [1.0, 3.0], [1.0, 4.0], [1.0, 5.0],
[2.0, 5.0], [3.0, 5.0], [4.0, 5.0], [5.0, 5.0], [5.0, 4.0],
[5.0, 3.0], [5.0, 2.0], [5.0, 1.0], [4.0, 1.0], [3.0, 1.0],
[2.0, 1.0], [1.0, 1.0]
]
# resolve max values for x and y axes
max_x = max([pt[0] for pt in lst]) 1
max_y = max([pt[1] for pt in lst]) 1
# create figure and set limits
fig = plt.figure()
plt.xlim(0, max_x)
plt.ylim(0, max_y)
# create graph
graph, = plt.plot([], [], 'o')
# hide figure
plt.close()
# define function for animation
# it sets point coordinates based an frame number
def animate(i):
graph.set_data(lst[i][0], lst[i][1])
return graph
# init FuncAnimation
ani = FuncAnimation(fig, animate, frames=len(lst), interval=200, repeat=False)
# is needed to make animation available in jupiter / colab
HTML(ani.to_jshtml())