【问题标题】:How to update values of python 2D array using loop and animate changing the colors如何使用循环更新 python 二维数组的值并动画更改颜色
【发布时间】:2022-11-28 18:08:50
【问题描述】:

我正在寻找一种更新数组值的方法 通过创建更新函数创建的 numpy 数组 更新前一个数组的值和 更改新值的颜色已更新

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()

``

【问题讨论】:

    标签: python arrays animation 2d


    【解决方案1】:

    必须是电影文件格式吗? 您或许可以使用 Plotly 动画功能,为每一帧创建一个新的热图:

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

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

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

    编辑:有人已经做了类似的事情(至少在早期版本中):

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

    【讨论】:

      猜你喜欢
      • 2013-05-22
      • 2012-12-13
      • 2015-08-02
      • 1970-01-01
      • 2019-11-05
      • 2012-11-08
      • 1970-01-01
      • 2019-04-25
      • 2020-10-13
      相关资源
      最近更新 更多