如何为从2d直方图派生的图像设置动画

时间:2018-01-23 06:05:02

标签: python image animation matplotlib scatter

我正在尝试创建散点图的动画以及2d直方图。我可以让散点图工作。我也可以创建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)

1 个答案:

答案 0 :(得分:0)

可以使用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()