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

时间:2022-12-27 21:17:20

I am trying to create an animation of a scatterplot as well as a 2d Histogram. I can get the scatter plot working. I can also create individual stills of the 2d Histogram but cannot get it to animate with the scatter plot.

我正在尝试创建散点图的动画以及2d直方图。我可以得到散点图。我也可以创建2d直方图的个别静止图像但不能使用散点图来设置动画。

I can create some mock data if that would help. Please find code below.

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

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 个解决方案

#1


0  

The image can be updated with im.set_data(data), where you need to call hist2d to get the updated data to pass to im. As a minimal example,

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

#1


0  

The image can be updated with im.set_data(data), where you need to call hist2d to get the updated data to pass to im. As a minimal example,

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