如何使用 python 创建一个一个一个地绘制散点图的视频?
How to create a video that plots the scatter plots one by one using python?
我正在将一些数据绘制为覆盖在图像上的散点图。我想通过一次绘制一个散点来制作动画。这是我现在使用和编辑来自 :
的答案的代码
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
x = random.sample(range(0, 287), 20)
y = random.sample(range(0, 380), 20)
size = [20 for x in range(20)]
colors = ["r" for x in range(20)]
cm = plt.get_cmap('jet')
fig = plt.figure(figsize=(18,9))
graph = plt.scatter([], [],marker='+')
url = 'https://raw.githubusercontent.com/kornelski/pngquant/master/test/img/test.png'
im = plt.imread(url)
def animate(i):
implot = plt.imshow(im)
graph.set_offsets(np.vstack((x[:i+1], y[:i+1])).T)
graph.set_sizes(size[:i])
graph.set_facecolors(colors[:i+1])
return graph
ani = FuncAnimation(fig, animate, repeat=False, interval=0.1)
plt.show()
有两件事我需要帮助。
- 我希望散点图的颜色根据第三个变量发生变化,即使用
cmap
。但是,set_facecolors不接受这样的说法。
- 当我尝试使用
ani.save('files/animation.gif',writer='imagemagick', fps=60)
保存我的动画时,我的 jupyter notebook 崩溃了。
有人可以帮助我吗?
图表的背景图是加ax绘制的。颜色图也是根据数据个数,20个创建,创建一个列表,这样可以显示每种颜色。由于图片的坐标和图形的坐标基础不同,所以y轴设置反方向
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import random
from PIL import Image
import urllib.request
random.seed(20210702)
N = 20
x = random.sample(range(0, 380), N)
y = random.sample(range(0, 287), N)
size = [20 for x in range(N)]
colors = []
cm = plt.get_cmap('jet', N)
fig,ax = plt.subplots(figsize=(9, 4.5))
plt.xlim(0, 380)
plt.ylim(287, 0)
graph = ax.scatter([], [], marker='+')#
url = 'https://raw.githubusercontent.com/kornelski/pngquant/master/test/img/test.png'
im = Image.open(urllib.request.urlopen(url))
print(im.size)
def animate(i):
ax.imshow(im)
graph.set_offsets(np.vstack((x[:i+1], y[:i+1])).T)
graph.set_sizes(size[:i+1])
colors.append(cm(i))
graph.set_facecolors(colors)
return graph
ani = FuncAnimation(fig, animate, frames=20, repeat=False, interval=200)
plt.show()
我正在将一些数据绘制为覆盖在图像上的散点图。我想通过一次绘制一个散点来制作动画。这是我现在使用和编辑来自
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
x = random.sample(range(0, 287), 20)
y = random.sample(range(0, 380), 20)
size = [20 for x in range(20)]
colors = ["r" for x in range(20)]
cm = plt.get_cmap('jet')
fig = plt.figure(figsize=(18,9))
graph = plt.scatter([], [],marker='+')
url = 'https://raw.githubusercontent.com/kornelski/pngquant/master/test/img/test.png'
im = plt.imread(url)
def animate(i):
implot = plt.imshow(im)
graph.set_offsets(np.vstack((x[:i+1], y[:i+1])).T)
graph.set_sizes(size[:i])
graph.set_facecolors(colors[:i+1])
return graph
ani = FuncAnimation(fig, animate, repeat=False, interval=0.1)
plt.show()
有两件事我需要帮助。
- 我希望散点图的颜色根据第三个变量发生变化,即使用
cmap
。但是,set_facecolors不接受这样的说法。 - 当我尝试使用
ani.save('files/animation.gif',writer='imagemagick', fps=60)
保存我的动画时,我的 jupyter notebook 崩溃了。
有人可以帮助我吗?
图表的背景图是加ax绘制的。颜色图也是根据数据个数,20个创建,创建一个列表,这样可以显示每种颜色。由于图片的坐标和图形的坐标基础不同,所以y轴设置反方向
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import random
from PIL import Image
import urllib.request
random.seed(20210702)
N = 20
x = random.sample(range(0, 380), N)
y = random.sample(range(0, 287), N)
size = [20 for x in range(N)]
colors = []
cm = plt.get_cmap('jet', N)
fig,ax = plt.subplots(figsize=(9, 4.5))
plt.xlim(0, 380)
plt.ylim(287, 0)
graph = ax.scatter([], [], marker='+')#
url = 'https://raw.githubusercontent.com/kornelski/pngquant/master/test/img/test.png'
im = Image.open(urllib.request.urlopen(url))
print(im.size)
def animate(i):
ax.imshow(im)
graph.set_offsets(np.vstack((x[:i+1], y[:i+1])).T)
graph.set_sizes(size[:i+1])
colors.append(cm(i))
graph.set_facecolors(colors)
return graph
ani = FuncAnimation(fig, animate, frames=20, repeat=False, interval=200)
plt.show()