Python-如何设置散点图的动画?

Python-如何设置散点图的动画?,python,numpy,matplotlib,Python,Numpy,Matplotlib,这是我目前的脚本: import numpy as np import matplotlib.pyplot as plt import random t=0 r=3.0 n=0 A=[] for x in range(10): for y in range(10): A.append([random.uniform(0,1),random.uniform(0,1)]) for m in range(len(A)): plt.plot(A[m][0],A[m][1], "x", c

这是我目前的脚本:

import numpy as np
import matplotlib.pyplot as plt
import random
t=0
r=3.0
n=0
A=[]
for x in range(10):
  for y in range(10):
    A.append([random.uniform(0,1),random.uniform(0,1)])
for m in range(len(A)):
  plt.plot(A[m][0],A[m][1], "x", color="blue")
plt.show()
while n<=100:
  for m in range(len(A)):
    A[m][0]=r*A[m][0]*(1-A[m][0])
    A[m][1]=r*A[m][1]*(1-A[m][1])
  for m in range(len(A)):
    plt.plot(A[m][0],A[m][1], "x", color="blue")
  plt.show()
  n+=1
将numpy导入为np
将matplotlib.pyplot作为plt导入
随机输入
t=0
r=3.0
n=0
A=[]
对于范围(10)内的x:
对于范围(10)内的y:
A.append([random.uniform(0,1),random.uniform(0,1)])
对于范围内的m(len(A)):
plt.绘图(A[m][0],A[m][1],“x”,color=“blue”)
plt.show()
n使用
plt.ion()
启用交互式打印(打开打印窗口时不会停止执行),然后使用
plt.clf()
清除打印

工作样本为:

import numpy as np
import matplotlib.pyplot as plt
plt.ion()

import random
t=0
r=3.0
n=0
A=[]
for x in range(10):
    for y in range(10):
        A.append([random.uniform(0,1),random.uniform(0,1)])

for m in range(len(A)):
    plt.plot(A[m][0],A[m][1], "x", color="blue")
    plt.draw()
plt.pause(1)

while n<=100:
    for m in range(len(A)):
        A[m][0]=r*A[m][0]*(1-A[m][0])
        A[m][1]=r*A[m][1]*(1-A[m][1])
    for m in range(len(A)):
        plt.plot(A[m][0],A[m][1], "x", color="blue")
    plt.draw()
    plt.pause(1)
    plt.clf()
将numpy导入为np
将matplotlib.pyplot作为plt导入
plt.ion()
随机输入
t=0
r=3.0
n=0
A=[]
对于范围(10)内的x:
对于范围(10)内的y:
A.append([random.uniform(0,1),random.uniform(0,1)])
对于范围内的m(len(A)):
plt.绘图(A[m][0],A[m][1],“x”,color=“blue”)
plt.draw()
plt.暂停(1)
n使用
plt.ion()
启用交互式打印(打开打印窗口时不会停止执行),然后使用
plt.clf()
清除打印

工作样本为:

import numpy as np
import matplotlib.pyplot as plt
plt.ion()

import random
t=0
r=3.0
n=0
A=[]
for x in range(10):
    for y in range(10):
        A.append([random.uniform(0,1),random.uniform(0,1)])

for m in range(len(A)):
    plt.plot(A[m][0],A[m][1], "x", color="blue")
    plt.draw()
plt.pause(1)

while n<=100:
    for m in range(len(A)):
        A[m][0]=r*A[m][0]*(1-A[m][0])
        A[m][1]=r*A[m][1]*(1-A[m][1])
    for m in range(len(A)):
        plt.plot(A[m][0],A[m][1], "x", color="blue")
    plt.draw()
    plt.pause(1)
    plt.clf()
将numpy导入为np
将matplotlib.pyplot作为plt导入
plt.ion()
随机输入
t=0
r=3.0
n=0
A=[]
对于范围(10)内的x:
对于范围(10)内的y:
A.append([random.uniform(0,1),random.uniform(0,1)])
对于范围内的m(len(A)):
plt.绘图(A[m][0],A[m][1],“x”,color=“blue”)
plt.draw()
plt.暂停(1)

而n则可以使用
matplotlib.animation
package:

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

t=0
r=3.0
n=0
A=[]

for x in range(10):
    for y in range(10):
        A.append([random.uniform(0,1),random.uniform(0,1)])
A = np.array(A).transpose()

fig = plt.figure()
line, = plt.plot(A[0],A[1], "x", color="blue")

def update():
    for i in range(100):
        A[0], A[1] = r*A[0]*(1-A[0]), r*A[1]*(1-A[1])
        yield A

def draw(data):
    line.set_xdata(data[0])
    line.set_ydata(data[1])
    return line,

ani = animation.FuncAnimation(fig, draw, update, interval=1000, blit=False)

plt.show()

update
函数是一个生成器,用于生成后续步骤的数据,
draw
是一个函数,用于更新打印数据并将其返回。

您可以使用
matplotlib.animation
软件包:

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

t=0
r=3.0
n=0
A=[]

for x in range(10):
    for y in range(10):
        A.append([random.uniform(0,1),random.uniform(0,1)])
A = np.array(A).transpose()

fig = plt.figure()
line, = plt.plot(A[0],A[1], "x", color="blue")

def update():
    for i in range(100):
        A[0], A[1] = r*A[0]*(1-A[0]), r*A[1]*(1-A[1])
        yield A

def draw(data):
    line.set_xdata(data[0])
    line.set_ydata(data[1])
    return line,

ani = animation.FuncAnimation(fig, draw, update, interval=1000, blit=False)

plt.show()

update
函数是一个生成器,它为后续步骤生成数据,而
draw
是一个更新绘图数据并返回它的函数。

我建议使用matplotlib的面向对象接口,在

这样,您就可以更好地控制图形行为,并可以在循环中简单地重新绘制绘图:

import numpy as np
import matplotlib.pyplot as plt
import random
from time import sleep
t=0
r=3.0
n=0
A=[]
for x in range(10):
    for y in range(10):
        A.append([random.uniform(0,1),random.uniform(0,1)])
fig = plt.figure()
ax = fig.add_subplot(111)
for m in range(len(A)):
    ax.plot(A[m][0],A[m][1], "x", color="blue")
fig.show()
sleep(1)
while n<=100:
    for m in range(len(A)):
        A[m][0]=r*A[m][0]*(1-A[m][0])
        A[m][1]=r*A[m][1]*(1-A[m][1])
    ax.clear()
    for m in range(len(A)):
        ax.plot(A[m][0],A[m][1], "x", color="blue")
    fig.canvas.draw()
    sleep(1)
    n+=1
将numpy导入为np
将matplotlib.pyplot作为plt导入
随机输入
从时间上导入睡眠
t=0
r=3.0
n=0
A=[]
对于范围(10)内的x:
对于范围(10)内的y:
A.append([random.uniform(0,1),random.uniform(0,1)])
图=plt.图()
ax=图添加_子批次(111)
对于范围内的m(len(A)):
ax.绘图(A[m][0],A[m][1],“x”,color=“blue”)
图2(图3)
睡眠(1)

而n我建议使用matplotlib的面向对象接口,在

这样,您就可以更好地控制图形行为,并可以在循环中简单地重新绘制绘图:

import numpy as np
import matplotlib.pyplot as plt
import random
from time import sleep
t=0
r=3.0
n=0
A=[]
for x in range(10):
    for y in range(10):
        A.append([random.uniform(0,1),random.uniform(0,1)])
fig = plt.figure()
ax = fig.add_subplot(111)
for m in range(len(A)):
    ax.plot(A[m][0],A[m][1], "x", color="blue")
fig.show()
sleep(1)
while n<=100:
    for m in range(len(A)):
        A[m][0]=r*A[m][0]*(1-A[m][0])
        A[m][1]=r*A[m][1]*(1-A[m][1])
    ax.clear()
    for m in range(len(A)):
        ax.plot(A[m][0],A[m][1], "x", color="blue")
    fig.canvas.draw()
    sleep(1)
    n+=1
将numpy导入为np
将matplotlib.pyplot作为plt导入
随机输入
从时间上导入睡眠
t=0
r=3.0
n=0
A=[]
对于范围(10)内的x:
对于范围(10)内的y:
A.append([random.uniform(0,1),random.uniform(0,1)])
图=plt.图()
ax=图添加_子批次(111)
对于范围内的m(len(A)):
ax.绘图(A[m][0],A[m][1],“x”,color=“blue”)
图2(图3)
睡眠(1)

然而,需要注意的是,动画模块保留了一份从
update
中提取的结果的副本,如果您的帧太大,这可能会导致内存问题。需要注意的是,动画模块保留了一份从
update
中提取的结果的副本,如果您的帧太大,这可能会导致内存问题太大了,谢谢!几处改动,它完全符合我的要求。同样感谢您提供的提示,numpy仍然是早期版本的遗留版本,当我使用numpy.linspace时,我忘记删除导入的numpy部分。谢谢!几处改动,它完全符合我的要求。同样感谢您提供的提示,numpy仍然是早期版本的遗留版本,当我使用numpy.linspace时,我忘记删除导入的numpy部分。