简体   繁体   English

Matplotlib 动画散点图如何在不叠加数据的情况下完成?

[英]How animation scatter plot with Matplotlib can be done with not superimposed data?

I want to do an animated scatter plot with one only pair of x,y data for each frame.我想做一个动画散点图,每帧只有一对 x,y 数据。

The code I wrote creates an animated scatter plot but the old dots appear in the plot, that means that new dots are added on the plot, keeping the old ones.我编写的代码创建了一个动画散点图,但旧点出现在图中,这意味着在图上添加了新点,保留了旧点。

For the code below I want a dot per frame like a moving dot on x axis and not adding one more value.对于下面的代码,我希望每帧有一个点,就像 x 轴上的移动点一样,而不是再增加一个值。

I tried with plt.clf() but then all data disappear.我尝试使用plt.clf()但随后所有数据都消失了。

%matplotlib notebook
from bokeh.plotting import figure, output_file, show
import pandas
import numpy as np
import matplotlib.animation as animation
from matplotlib.animation import FuncAnimation, PillowWriter
import matplotlib.pyplot as plt

writer = PillowWriter(fps=10)

list_x=[1,2,3,4,5,6,7,8,9]
list_y=[5,5,5,5,5,5,5,5,5]

def plot(listax, listay):
    
    plt.scatter(listax, listay, c='blue', alpha=0.5)

    plt.show()

fig2 = plt.figure()
plt.xlim([0, 10])
plt.ylim([0, 10])
with writer.saving(fig2, "plotvideo.gif", 100):
    for i in range(0, len(list_x)):
        x_value = list_x[i]
        y_value = list_y[i]
        
        writer.grab_frame()
        plot(x_value, y_value)

Use the .remove() method on the point objects to remove them from the figure.对点对象使用 .remove() 方法将它们从图中删除。

I would try this:我会试试这个:

from bokeh.plotting import figure, output_file, show
import pandas
import numpy as np
import matplotlib.animation as animation
from matplotlib.animation import FuncAnimation, PillowWriter
import matplotlib.pyplot as plt
import time

writer = PillowWriter(fps=10)

list_x=[1,2,3,4,5,6,7,8,9]
list_y=[5,5,5,5,5,5,5,5,5]
points = []

def plot(listax, listay, j):
    
    points.append(plt.scatter(listax[j], listay[j], c='blue', alpha=0.5))
    if len(points) == 2:
        points[0].remove()
        points.pop(0)
    plt.show(block=False)

fig2 = plt.figure()
plt.xlim([0, 10])
plt.ylim([0, 10])
with writer.saving(fig2, "plotvideo.gif", 100):
    for i in range(0, len(list_x)):
        x_value = list_x
        y_value = list_y
        
        writer.grab_frame()
        print(points)
        plot(x_value, y_value, i)

See this link for a better explanation (albeit with a different implementation): How to remove points from a plot?请参阅此链接以获得更好的解释(尽管有不同的实现): 如何从图中删除点?

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

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