Home > Enterprise >  How to update values of python 2D array using loop and animate changing the colors
How to update values of python 2D array using loop and animate changing the colors

Time:11-30

I am looking for a way to update the values of the array numphy array created by creating an update function to update the values of the previous array and change the colors of the new values updated

below is my code though it only display the final frame.
My Question is how do i display the entire process to show how each cell satisfying the condition given change color step by step. 
from tkinter import N

import args as args
import numpy as np
from matplotlib import pyplot as plt, animation
from matplotlib import colors

# setting up the values for the grid
ON = 1
OFF = 0
vals = [ON, OFF]

data = np.array([
    [0,0,0,0,1,1,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0],
    [0,0,2,0,0,0,0,2,0,0],
    [0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,2,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0]
])
def update(previous_array):
    for row in range(previous_array):
        for cell in range(row):
            if cell > 1:
                color = 'red'
                return previous_array

cmap = colors.ListedColormap(['Blue','red'])
# plt.figure(figsize=(6,6))
fig, ax = plt.subplots()
# im = ax.imshow(data, cmap=cmap)
plt.pcolor(data[::-1], cmap=cmap, edgecolors='k', linewidths=2)
for i in range(len(data)):
    for j in range(len(data[1])):
        color = 'red' if data[i, j] == 0 else 'blue'

ani = animation.FuncAnimation(fig, update,
                                  frames=30,
                                  interval=50,
                                  save_count=50)
ani.save('basic_animation.mp4', fps=30, extra_args=['-vcodec', 'libx264'])

plt.show()

``

CodePudding user response:

Does it need to be a movie file format? You can probably use the Plotly animation feature, where you create a new heatmap for each frame:

https://plotly.com/python/animations/

https://plotly.com/python/visualizing-mri-volume-slices/

https://plotly.com/python/heatmaps/

edit: someone did something similar already (at least in an earlier version):

https://plotly.com/python/v3/heatmap-animation/

The example below takes your data and some altered versions into an animation. You shoud replace the list of frames of data by your own list (pre-generate these results).

import plotly.graph_objects as go
import numpy as np

data = np.array([
    [0,0,0,0,1,1,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0],
    [0,0,2,0,0,0,0,2,0,0],
    [0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,2,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0]
    ])

data2 = data.copy()
data3 = data.copy()
data4 = data.copy()
data5 = data.copy()

data2[data2 == 1] = 3
data3[data3 == 2] = 0
data4[data4 == 0] = 4
data5[data5 > 0] = 2

fig = go.Figure(
    data=[go.Heatmap(z=data*0)],
    layout=go.Layout(
        xaxis=dict(range=[0, data.shape[1]-1],
                   dtick=1),
        yaxis=dict(range=[0, data.shape[0]-1],
                   dtick=1),
        title="Start Title",
        updatemenus=[dict(
            type="buttons",
            buttons=[dict(label="Play",
                          method="animate",
                          args=[None])])]
    ),
    frames=[go.Frame(data=[go.Heatmap(z=data)],
                     layout=go.Layout(title_text="Title1")),
            go.Frame(data=[go.Heatmap(z=data2)],
                     layout=go.Layout(title_text="Title2")),
            go.Frame(data=[go.Heatmap(z=data3)],
                     layout=go.Layout(title_text="Title3")),
            go.Frame(data=[go.Heatmap(z=data4)],
                     layout=go.Layout(title_text="Title4")),
            go.Frame(data=[go.Heatmap(z=data5)],
                     layout=go.Layout(title_text="Title5"))]
)

fig.show()
  • Related