【问题标题】:How to make points in plt.scatter() appear one at a time on display?如何使 plt.scatter() 中的点一次显示一个?
【发布时间】:2020-08-25 11:43:17
【问题描述】:

我正在使用this tutorial of the Barnsley fern 中的代码,我希望能够缓慢地绘制蕨类植物(而不是一次)。 Python 还不是很舒服,我看到函数 plt.pause() 可能会在其他一些答案中起到作用;但是,我不知道如何将 plt.pause() 与 plt.scatter() 结合起来以获得动画 gif 效果。例如,我是否必须将 plt.scatter 合并到 for 循环中?

# importing necessary modules 
import matplotlib.pyplot as plt 
from random import randint 

# initializing the list 
x = [] 
y = [] 

# setting first element to 0 
x.append(0) 
y.append(0) 

current = 0

for i in range(1, 50000): 

    # generates a random integer between 1 and 100 
    z = randint(1, 100) 

    # the x and y coordinates of the equations 
    # are appended in the lists respectively. 

    # for the probability 0.01 
    if z == 1: 
        a = 0; b = 0; c = 0; d = 0.16; e = 0; f = 0
        x.append(0) 
        y.append(d*(y[current])) 

    # for the probability 0.85     
    if z>= 2 and z<= 86: 
        a = 0.85; b = 0.04; c = -0.04; d = 0.85; e = 0; f = 1.6
        x.append(a*(x[current]) + b*(y[current])) 
        y.append(c*(x[current]) + d*(y[current])+f) 

    # for the probability 0.07     
    if z>= 87 and z<= 93: 
        a = 0.2; b = -0.26; c = 0.23; d = 0.22; e = 0; f = 1.6
        x.append(a*(x[current]) + b*(y[current])) 
        y.append(c*(x[current]) + d*(y[current])+f) 

    # for the probability 0.07     
    if z>= 94 and z<= 100: 
        a = -0.15; b = 0.28; c = 0.26; d = 0.24; e = 0; f = 0.44
        x.append(a*(x[current]) + b*(y[current])) 
        y.append(c*(x[current]) + d*(y[current])+f) 

    current = current + 1

plt.figure(figsize=(10,10))

plt.scatter(x, y, s = 0.2, edgecolor ='green') 

这是想要的效果:

【问题讨论】:

    标签: python matplotlib plot


    【解决方案1】:

    我建议你使用FuncAnimation from matplotlib.animation:

    你可以在每一帧中只取一个点。您只需设置轴限制以避免图形移动:

    fig, ax = plt.subplots(figsize=(10, 10))
    ax.set_xlim(min(x), max(x))
    ax.set_ylim(min(y), max(y))
    
    def animation(i):
        ax.scatter(x[i], y[i], s=0.2, edgecolor='green')
    
    
    ani = FuncAnimation(fig, animation, frames=len(x))
    
    plt.show()
    

    编辑 如果您想同时绘制更多点,例如15:

    fig, ax = plt.subplots(figsize=(10, 10))
    ax.set_xlim(min(x), max(x))
    ax.set_ylim(min(y), max(y))
    
    nb_points = 15
    # calculate number of frames needed:
    # one more frame if remainder is not 0
    nb_frames = len(x) // nb_points + (len(x) % nb_points != 0)
    
    
    def animation(i):
        i_from = i * nb_points
        # are we on the last frame?
        if i_from + nb_points > len(x) - 1:
            i_to = len(x) - 1
        else:
            i_to = i_from + nb_points
        ax.scatter(x[i_from:i_to], y[i_from:i_to], s=0.2, edgecolor='green')
    
    
    ani = FuncAnimation(fig, animation, frames=nb_frames)
    
    plt.show()
    

    【讨论】:

    • 我确定我错过了一些东西,但它只显示了一个带有第一批点的数字。
    • 我确实有正确的视觉输出...您尝试过更少的步骤吗?任何控制台输出?
    • 我正在使用带有绘图内联输出的 Jupyter NB。您介意粘贴您运行的整个代码吗?谢谢!
    • 我是实时看到的,但我不使用 Jupyter 来做。我想你可以试试https://stackoverflow.com/questions/43445103/inline-animations-in-jupyter#43447370
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-07
    • 1970-01-01
    • 1970-01-01
    • 2017-06-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多