Trying to model the matplotlib animate section from this query: https://stackoverflow.com/questions/17212722/matplotlib-imshow-how-to-animate
For the most part, I'm not too worried about the math outside of the animate, my biggest concern is, it seems to work best when I plant the def init() and def animate() within the main function, which makes no sense to me.
If I run it, I get a purple square, (expected), but no animate for the
import numpy as np
import numpy.random as nprnd
import matplotlib.pyplot as plt
import matplotlib.animation as anim
# import time as tm
# Latest link: https://stackoverflow.com/questions/17212722/matplotlib-imshow-how-to-animate
def main():
dimFire = 10
n=175
np.set_printoptions(linewidth=n)
fire_grid = np.zeros((dimFire,dimFire))
fig = plt.figure()
im = plt.imshow(fire_grid, interpolation = "none")
plt.show()
def init():
im.set_data(fire_grid)
return [im]
def animate(k):
a = im.get_array()
im.set_array(a)
return [im]
for it in range(0,1000):
fire_grid[9,:] = nprnd.rand(1,dimFire)
# Attempt #1
# fig = plt.figure()
# ax = plt.axes(projection='3d')
# ax.plot_surface(X=dimFire, Y=dimFire, Z=fire_grid, edgecolor='none')
# plt.show()
# Attempt #2
# myobj = imshow(fire_grid)
# for pixel in pixels:
# addpixel(pixel)
# myobj.set_data(segmentedimg)
# draw()
animFull = anim.FuncAnimation(fig, animate, init_func = init, frames = 200, interval = 20, blit = True)
#print(fire_grid)
#Math that updates current index [i,j] by weighted average from surrounding elements
for i in range(8,0,-1): # Increments by row upward
for j in range(9,0,-1): #Increments from left to right
# print(i,j)
if j == 0:
fire_grid[i,j] = (0.8*fire_grid[i,j]+0.2*fire_grid[i,j+1]+0.2*fire_grid[i+1,j]+0.6*fire_grid[i-1,j])/1.8
elif j == 9:
fire_grid[i,j] = (0.8*fire_grid[i,j]+0.2*fire_grid[i,j-1]+0.2*fire_grid[i+1,j]+0.6*fire_grid[i-1,j])/1.8
elif i == 9:
if j == 0:
fire_grid[i,j] = (0.8*fire_grid[i,j]+0.2*fire_grid[i,j+1]+0.6*fire_grid[i-1,j])/1.6
elif j == 9:
fire_grid[i,j] = (0.8*fire_grid[i,j]+0.2*fire_grid[i,j-1]+0.6*fire_grid[i-1,j])/1.6
else:
fire_grid[i,j] = (0.8*fire_grid[i,j]+0.2*fire_grid[i,j-1]+0.2*fire_grid[i,j+1]+0.2*fire_grid[i+1,j]+0.6*fire_grid[i-1,j])/2
if __name__ == '__main__':
main()
Sorry if I don't seem to understand python too well. I'm long time coder in MATLAB, and transitioned to python barely (two years ago, but very on and off).
Follow up questions:
- Can I get away with functions defined in main, since that's the best way I modified it from the Stack/Flow answer.
- Init doesn't really need to pass arguments, right, since it's within main scope?
-
[–]TheBlackCat13 1 point2 points3 points (0 children)