【问题标题】:Matplotlib, animation by points in listMatplotlib,列表中的点动画
【发布时间】:2021-09-25 02:54:31
【问题描述】:

我想要一个点列表

list = [[1.0, 1.0], ... ,[17.3, 39.4]]

我想在 matplotlib 中制作动画,我的对象将在第一帧中的第一个点,第二个点的第二帧,依此类推...

我该怎么做?

我只是想知道大致的想法.. 可能的最基本代码,因为我是这个 python 库的新手。

【问题讨论】:

  • 很多很好的例子here

标签: python python-3.x python-2.7 matplotlib animation


【解决方案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())

【讨论】:

    猜你喜欢
    • 2021-04-02
    • 1970-01-01
    • 2021-10-21
    • 2021-11-05
    • 1970-01-01
    • 2023-01-08
    • 2012-07-07
    • 2020-09-21
    • 2017-08-20
    相关资源
    最近更新 更多