简体   繁体   English

使用 matplotlib 动画散点

[英]Animating scatter points with matplotlib

I created a script that animates a line and scatters points, here is the script:我创建了一个动画线和散点的脚本,这里是脚本:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

fig, ax = plt.subplots(figsize=(16,8))
ax.set(xlim=(0,104), ylim=(0,68))

x_start, y_start = (50, 35)
x_end, y_end = (90, 45)

x = np.linspace(x_start, x_end, 20)
y = np.linspace(y_start, y_end, 20)

## the first scatter point
sc_1 = ax.scatter([], [], color="crimson", zorder=4, s=150, alpha=0.7, edgecolor="w")

## the line
line, = ax.plot([], [], color="black", zorder=4)

## the last scatter point
sc_2 = ax.scatter([], [], color="crimson", zorder=4, s=150, alpha=0.7, edgecolor="w")

## titles
title = ax.text(50, 65, "", bbox={'facecolor':'w', 'alpha':0.5, 'pad':5}, ha="center")

## scatter point which will follow the line
sc_3 = ax.scatter([], [], color="crimson", zorder=4, s=150, edgecolor="w")

def animate(i):
    if i == 1:
       sc_1.set_offsets([x_start, y_start])
       title.set_text("Action 001")

    if i == 2:
        plt.pause(0.5)
        title.set_text("Action 002")

    ## plot line
    line.set_data(x[:i], y[:i])

    ## plot scatter point that will follow the line
    sc_3.set_offsets(np.c_[x[:i], y[:i]])
    
    ## plot scatter point
    if i > len(x):

        plt.pause(0.2)
        title.set_text("Action 003")
        sc_2.set_offsets([x_end, y_end])

    return sc_1, line, sc_2, title, sc_3

ani = animation.FuncAnimation(  
    fig=fig, func=animate, interval=50, blit=True)  

ani.save("a.gif", writer="imagemagick")

plt.show()

It is giving the following output:它提供以下输出:

在此处输入图片说明

But what I want is while animating the scatter point using sc_3 the program should remove the last iteration of scatter point produced by sc_3 , ie scatter point should not be plotted over the whole line, just a scatter point which will follow the line from start to the end.但是我想要的是在使用sc_3为散点设置动画时,程序应该删除由sc_3产生的散点的最后一次迭代,即散点不应绘制在整条线上,而只是一个散点,它将sc_3跟随这条线结束。

The final output is like: scatter point at the start of the line and then a scatter point will follow the line(removing its last state position) and a final scatter point at the end of the line最终的输出是这样的:行首的散点,然后散点将跟随行(删除其最后的状态位置)和行尾的最终散点

What should be added in the program to get the desired output?应该在程序中添加什么以获得所需的输出?

I have slightly edited your code as following:我稍微编辑了您的代码如下:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

fig, ax = plt.subplots(figsize = (16, 8))
ax.set(xlim = (0, 104), ylim = (0, 68))

x_start, y_start = (50, 35)
x_end, y_end = (90, 45)

N = 20
x = np.linspace(x_start, x_end, N)
y = np.linspace(y_start, y_end, N)

## the first scatter point
sc_1 = ax.scatter([], [], color = "crimson", zorder = 4, s = 150, alpha = 0.7, edgecolor = "w")

## the line
line, = ax.plot([], [], color = "black", zorder = 4)

## the last scatter point
sc_2 = ax.scatter([], [], color = "crimson", zorder = 4, s = 150, alpha = 0.7, edgecolor = "w")

## titles
title = ax.text(50, 65, "", bbox = {'facecolor': 'w', 'alpha': 0.5, 'pad': 5}, ha = "center")

## scatter point which will follow the line
sc_3 = ax.scatter([], [], color = "crimson", zorder = 4, s = 150, edgecolor = "w")

def animate(i):

    if i == 1:
        sc_1.set_offsets([x_start, y_start])
        title.set_text("Action 001")

    if i == 2:
        plt.pause(0.5)
        title.set_text("Action 002")

    ## plot line and scatter point that will follow the line
    if i >= 2:
        line.set_data(x[:i + 1], y[:i + 1])
        sc_3.set_offsets([x[i], y[i]])

    # plot scatter point
    if i == N - 1:
        plt.pause(0.2)
        title.set_text("Action 003")
        sc_2.set_offsets([x_end, y_end])

    return sc_1, line, sc_2, title, sc_3,


ani = animation.FuncAnimation(fig = fig, func = animate, frames = N, interval = 50, blit = True, repeat = False)
ani.save("a.gif", writer = "imagemagick")

plt.show()

The output animation is:输出动画为:

1

I tried this:我试过这个:

    ## plot scatter point that will follow the line
    sc_3.set_offsets(np.c_[x[i], y[i]])

The end result is that it retains a single red point, but animates a single point moving along with the line.最终结果是它保留了一个红点,但动画一个点随着线移动。

Edit : the first red point is given by sc_1, which I assume you want to retain.编辑:第一个红点由 sc_1 给出,我假设您想保留它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM