python - 如何对从二维直方图派生的图像进行动画处理

标签 python image animation matplotlib scatter

我正在尝试创建散点图和二维直方图的动画。我可以让散点图发挥作用。我还可以创建 2d 直方图的单独静态图像,但无法使用散点图对其进行动画处理。

如果有帮助的话,我可以创建一些模拟数据。请在下面找到代码。

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

#Create empty lists
visuals = [[],[],[]]

#This dataset contains XY coordinates from 21 different players derived from a match
with open('Heatmap_dataset.csv') as csvfile :
readCSV = csv.reader(csvfile, delimiter=',')
n=0
for row in readCSV :
    if n == 0 :
        n+=1
        continue
    #All I'm doing here is appending all the X-Coordinates and all the Y-Coordinates. As the data is read across the screen, not down.
    visuals[0].append([float(row[3]),float(row[5]),float(row[7]),float(row[9]),float(row[11]),float(row[13]),float(row[15]),float(row[17]),float(row[19]),float(row[21]),float(row[23]),float(row[25]),float(row[27]),float(row[29]),float(row[31]),float(row[33]),float(row[35]),float(row[37]),float(row[39]),float(row[41]),float(row[43])])
    visuals[1].append([float(row[2]),float(row[4]),float(row[6]),float(row[8]),float(row[10]),float(row[12]),float(row[14]),float(row[16]),float(row[18]),float(row[20]),float(row[22]),float(row[24]),float(row[26]),float(row[28]),float(row[30]),float(row[32]),float(row[34]),float(row[36]),float(row[38]),float(row[40]),float(row[42])])
    visuals[2].append([1,2])

#Create a list that contains all the X-Coordinates and all the Y-Coordinates. The 2nd list indicates the row. So visuals[1][100] would be the 100th row. 
Y = visuals[1][0]
X = visuals[0][0]

fig, ax = plt.subplots(figsize = (8,8))
plt.grid(False)

# Create scatter plot
scatter = ax.scatter(visuals[0][0], visuals[1][0], c=['white'], alpha = 0.7, s = 20, edgecolor = 'black', zorder = 2)

#Create 2d Histogram
data = (X, Y)
data,x,y,p = plt.hist2d(X,Y, bins = 15, range = np.array([(-90, 90), (0, 140)]))

#Smooth with filter
im = plt.imshow(data.T, interpolation = 'gaussian', origin = 'lower', extent = [-80,80,0,140])

ax.set_ylim(0,140)
ax.set_xlim(-85,85)

#Define animation. 
def animate(i) :
    scatter.set_offsets([[[[[[[[[[[[[[[[[[[[[visuals[0][0+i][0], visuals[1][0+i][0]], [visuals[0][0+i][1], visuals[1][0+i][1]], [visuals[0][0+i][2], visuals[1][0+i][2]], [visuals[0][0+i][3], visuals[1][0+i][3]], [visuals[0][0+i][4], visuals[1][0+i][4]],[visuals[0][0+i][5], visuals[1][0+i][5]], [visuals[0][0+i][6], visuals[1][0+i][6]], [visuals[0][0+i][7], visuals[1][0+i][7]], [visuals[0][0+i][8], visuals[1][0+i][8]], [visuals[0][0+i][9], visuals[1][0+i][9]], [visuals[0][0+i][10], visuals[1][0+i][10]], [visuals[0][0+i][11], visuals[1][0+i][11]], [visuals[0][0+i][12], visuals[1][0+i][12]], [visuals[0][0+i][13], visuals[1][0+i][13]], [visuals[0][0+i][14], visuals[1][0+i][14]], [visuals[0][0+i][15], visuals[1][0+i][15]], [visuals[0][0+i][16], visuals[1][0+i][16]], [visuals[0][0+i][17], visuals[1][0+i][17]], [visuals[0][0+i][18], visuals[1][0+i][18]], [visuals[0][0+i][19], visuals[1][0+i][19]], [visuals[0][0+i][20], visuals[1][0+i][20]]]]]]]]]]]]]]]]]]]]]])
# This is were I'm having trouble...How do I animate the image derived from the 2d histogram 
    im.set_array[i+1]

ani = animation.FuncAnimation(fig, animate, np.arange(0,1000),
                          interval = 100, blit = False)

最佳答案

可以使用im.set_data(data)更新图像,其中您需要调用hist2d来获取更新的数据以传递给im。作为一个最小的例子,

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

X = np.random.randn(100000)
Y = np.random.randn(100000) + 5

fig, ax = plt.subplots(figsize = (8,8))

#Create 2d Histogram
data,x,y = np.histogram2d(X,Y, bins = 15)

#Smooth with filter
im = plt.imshow(data.T, interpolation = 'gaussian', origin = 'lower')

#Define animation. 
def animate(i) :
    X = np.random.randn(100000)
    Y = np.random.randn(100000) + 5
    data,x,y = np.histogram2d(X,Y, bins = 15)
    im.set_data(data)

ani = animation.FuncAnimation(fig, animate, np.arange(0,1000),
                          interval = 100, blit = False)

plt.show()

关于python - 如何对从二维直方图派生的图像进行动画处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48395209/

相关文章:

python - 无法使用 Google BigQuery 进行身份验证

python - 在 DataFrame 底部添加一行以获得总计

python - eval 在评估生成器时未能检测到迭代变量

image - 从ttf文件中提取字体字符图像

html - SVG动画(SMIL),如何重复整个动画?

python - 对单个句子进行预测时,收到错误 "Number of features of the model must match the input."

image - Azure:对 BLOB 存储的请求会占用 CPU 吗?

ios - Lottie:无法获取带名称的动画

r - 动画包无法找到带有 convert = "convert"的 ImageMagick

Flutter Gallery 图片全屏可缩放