I don't have much experience in using matplotlib(and numpy), so this may be a beginner mistake. I'm trying to a wildfire simulation where fire progressively spreads on a 2D grid. It happens in intervals. A fire will spread to all adjacent square that is not already on fire. Before each interval, I copy the current array into a new array. Then, using the location of fires in the old array I make changes to the new array (to prevent infinite spreading in one interval). Finally, I take the new array and push is through the same process for a total of 15 times.
I'm currently having trouble printing onto a grid (1 = water, 2 = fire, 3 = tree). I followed Python - Plotting colored grid based on values. It works but, it only produces an image of the original data (which was a random grid) when it appears after the grid had been edited 15 times.
I ran code and confirmed that the state of the array before being plotted was vastly different from its very first iteration, regardless it still prints the first iteration.
Refer to the code (highlighted where the primary issue is)
import matplotlib.pyplot as plt
from matplotlib import colors
import numpy as np
from matplotlib.animation import PillowWriter
# 1 = blue, 2 = orange, 3 = green
def findColor(num):
if num == 1:
return 1
elif num == 2:
return 2
else:
return 3
data = np.random.randint(1, 11, size=(30, 30))
data = np.pad(data, pad_width=2, mode='constant',
constant_values=0)
n, m = data.shape
for x in range(2, n - 2):
for y in range(2, m - 2):
data[x, y] = findColor(data[x, y])
# Where the real issue is -----------------------------------------
for _ in range(15):
newCopy = np.copy(data)
for x in range(2, n - 2):
for y in range(2, m - 2):
node = data[x, y]
if node == 2: # If node is on fire
for p in [[x - 1, y], [x + 1, y], [x, y - 1], [x, y + 1]]: # Directly Adjacent Nodes
i, j = p
other = data[i, j]
if other == 3: # A tree that is not on fire
newCopy[i, j] = 2 # Tree is now on fire
data = np.copy(newCopy)
cmap = colors.ListedColormap(['blue', 'orange', 'green'])
fig, ax = plt.subplots()
ax.imshow(data, cmap=cmap)
ax.grid(which='major', axis='both', linestyle='-', color='k', linewidth=2)
ax.set_xticks(np.arange(-.5, 34, 1))
ax.set_yticks(np.arange(-.5, 34, 1))
plt.show()
Any help would be appreciated. Thanks in Advance
As mentioned prior I looked at the information inside the data before it was plotted. When it was plotted it didn't look at all like it was suppose to. I don't have any ideas for why this is happening.