Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python matplot动画绘制网格颜色的变化_Python_Matplotlib_Animation - Fatal编程技术网

如何使用python matplot动画绘制网格颜色的变化

如何使用python matplot动画绘制网格颜色的变化,python,matplotlib,animation,Python,Matplotlib,Animation,我想创建一个简单的动画来显示我的数据更改 创建一个3*3的网格 我有一个20*9的数组。数据逐行读取到动画中 我希望网格列表是逐行读取的,每行工作100毫秒。如果数字大于0,栅格的颜色将变为红色。例如,在前100毫秒中,所有颜色都是黑色,然后在第二个100毫秒中,网格编号0和网格编号4变为红色,并持续100毫秒。在第三个100毫秒,网格编号0,2,4,5变为红色,持续100毫秒 我当前版本的代码如下所示。我不知道如何像上面描述的那样画画 %matplotlib notebook ## s

我想创建一个简单的动画来显示我的数据更改

  • 创建一个3*3的网格
  • 我有一个20*9的数组。数据逐行读取到动画中
  • 我希望网格列表是逐行读取的,每行工作100毫秒。如果数字大于0,栅格的颜色将变为红色。例如,在前100毫秒中,所有颜色都是黑色,然后在第二个100毫秒中,网格编号0和网格编号4变为红色,并持续100毫秒。在第三个100毫秒,网格编号0,2,4,5变为红色,持续100毫秒

    我当前版本的代码如下所示。我不知道如何像上面描述的那样画画

    %matplotlib notebook     ## show in jupyter
    
    import numpy as np
    from matplotlib import pyplot as plt
    from matplotlib import animation
    
    
    fig = plt.figure()
    fig.set_dpi(100)
    fig.set_size_inches(7, 6.5)
    
    ax = plt.axes(xlim=(0, 10), ylim=(0, 10))
    patch = plt.Circle((5, -5), 0.75, fc='y')
    
    def init():
        patch.center = (5, 5)
        ax.add_patch(patch)
        return patch,
    
    def animate(i):
        x, y = patch.center
        x = 5 + 3 * np.sin(np.radians(i))
        y = 5 + 3 * np.cos(np.radians(i))
        patch.center = (x, y)
        return patch,
    
    anim = animation.FuncAnimation(fig, animate, 
                                   init_func=init, 
                                   frames=360, 
                                   interval=20,
                                   blit=True)
    
    plt.show()
    

    如果有人能帮助我,我将不胜感激

    好问题!我有一个有效的答案,但它改变了
    颜色列表的结构。下面是答案和解释

    %matplotlib notebook
    
    import numpy as np
    from matplotlib import pyplot as plt
    from matplotlib import animation
    
    
    fig = plt.figure()
    fig.set_dpi(100)
    fig.set_size_inches(7, 6.5)
    
    ax = plt.axes(xlim=(0, 10), ylim=(0, 10))
    patch = plt.Circle((5, -5), 0.75, fc='y')
    
    colors = [
        [["black", None, None], ["black", None, "black"], [None, None, "red"]],
        [["black", None, None], ["black", None, "red"], ["black", None, "red"]],
        [["red", None, None], [None, None, None], [None, None, "red"]],
    ]
    
    def get_coords(colors):
        y = 0
        x = 0
        coords = []
        for row in colors:
            x = 0
            for entry in row:
                if entry:
                    coords.append([entry, x, y])
                    x += 3.33
            y += 3.33
        return coords
    
    def get_grids(coord):
        return [plt.Rectangle((x[1], x[2]), 3.33, 3.33, fc=x[0]) for x in coord]
    
    coords = [get_coords(color) for color in colors]
    grids = [get_grids(coord) for coord in coords]
    
    def init():
        patch.center = (5, 5)
        ax.add_patch(patch)
        return patch,
    
    def animate(i):
        patches = []
        if (i % 100 == 0):
            ax.patches = []
            next_grid = grids.pop(0)
            for rectangle in next_grid:
                patches.append(ax.add_patch(rectangle))
    
        x, y = patch.center
        x = 5 + 3 * np.sin(np.radians(i))
        y = 5 + 3 * np.cos(np.radians(i))
        patch.center = (x, y)
        patches.append(ax.add_patch(patch))
        return patches
    
    anim = animation.FuncAnimation(fig, animate, 
                                   init_func=init, 
                                   frames=360, 
                                   interval=20,
                                   blit=True)
    
    plt.show()
    
    关键思想是添加和删除
    plt.矩形
    ,以提供网格的外观。因为图形是10x10,所以这些矩形是边长为10/3=~3.33的正方形

    我认为使用20*3*3比使用20*9的颜色列表更容易。我使用以下方法:

    colors = [
        [["black", None, None], ["black", None, "black"], [None, None, "red"]],
        [["black", None, None], ["black", None, "red"], ["black", None, "red"]],
        [["red", None, None], [None, None, None], [None, None, "red"]],
    ]
    
    此列表中的每个条目和您的一样,都是一个网格。但是,在这些网格中有行,每个行都有所需的颜色条目<代码>颜色[0][0]=[“黑色”,无,无]
    表示在动画的第一帧,网格的左下角将是黑色,而底部的其余部分将是透明的<代码>颜色[0][1]=[“黑色”,无,“黑色”]表示网格中间将有左三分之一和右三分之一的黑色,中间透明

    get_-coords
    get_-grids
    函数非常复杂,并且明确地进行了硬编码,以支持具有尺寸
    10x10
    3x3
    网格-将其参数化是很酷的

    另一个重要的想法是,要每100毫秒更改一次动画,我们只需检查animate函数中的
    i
    是否可以被100整除。如果是的话,我们清除现有的补丁(因此我们不仅仅是添加矩形),然后绘制新的补丁。当栅格列表用完时,栅格对于动画的其余部分将是透明的

    希望这有帮助-快乐策划

    import numpy as np
    from matplotlib import pyplot as plt
    from matplotlib import animation
    
    Color = [[0,0,0,0,0,0,0,0,0],
            [100,0,0,0,100,0,0,0,0,0],
            [80,0,80,0,80,100,0,0,0]]
    
    fig = plt.figure()
    fig.set_dpi(100)
    fig.set_size_inches(7, 6.5)
    fig.set_tight_layout(True)
    
    ax = plt.axes(xlim=(0, 6), ylim=(0, 6))
    patch = []
    
    patch.append(plt.Rectangle(xy = (0,0),width = 2,height = 2,fill = True,color = 'k'))
    patch.append(plt.Rectangle(xy = (2,0),width = 2,height = 2,fill = True,color = 'k'))
    patch.append(plt.Rectangle(xy = (4,0),width = 2,height = 2,fill = True,color = 'k'))
    patch.append(plt.Rectangle(xy = (0,2),width = 2,height = 2,fill = True,color = 'k'))
    patch.append(plt.Rectangle(xy = (2,2),width = 2,height = 2,fill = True,color = 'k'))
    patch.append(plt.Rectangle(xy = (4,2),width = 2,height = 2,fill = True,color = 'k'))
    patch.append(plt.Rectangle(xy = (0,4),width = 2,height = 2,fill = True,color = 'k'))
    patch.append(plt.Rectangle(xy = (2,4),width = 2,height = 2,fill = True,color = 'k'))
    patch.append(plt.Rectangle(xy = (4,4),width = 2,height = 2,fill = True,color = 'k'))
    
    def init():
        for i in range(9):
            patch[i].set_color('k')
            ax.add_patch(patch[i])
        return patch
    
    def animate(i):
        value = (np.array(Color[i]) == 0)
        for j in range(9):
            patch[j].set_color('k' if value[j] else 'r')
    
        return patch
    
    anim = animation.FuncAnimation(fig, animate,  
                                    init_func=init, 
                                    frames=3, 
                                    interval=1000,
                                    blit=True)
    
    plt.show()
    
    import numpy as np
    from matplotlib import pyplot as plt
    from matplotlib import animation
    
    Color = [[0,0,0,0,0,0,0,0,0],
            [100,0,0,0,100,0,0,0,0,0],
            [80,0,80,0,80,100,0,0,0]]
    
    fig = plt.figure()
    fig.set_dpi(100)
    fig.set_size_inches(7, 6.5)
    fig.set_tight_layout(True)
    
    ax = plt.axes(xlim=(0, 6), ylim=(0, 6))
    patch = []
    
    patch.append(plt.Rectangle(xy = (0,0),width = 2,height = 2,fill = True,color = 'k'))
    patch.append(plt.Rectangle(xy = (2,0),width = 2,height = 2,fill = True,color = 'k'))
    patch.append(plt.Rectangle(xy = (4,0),width = 2,height = 2,fill = True,color = 'k'))
    patch.append(plt.Rectangle(xy = (0,2),width = 2,height = 2,fill = True,color = 'k'))
    patch.append(plt.Rectangle(xy = (2,2),width = 2,height = 2,fill = True,color = 'k'))
    patch.append(plt.Rectangle(xy = (4,2),width = 2,height = 2,fill = True,color = 'k'))
    patch.append(plt.Rectangle(xy = (0,4),width = 2,height = 2,fill = True,color = 'k'))
    patch.append(plt.Rectangle(xy = (2,4),width = 2,height = 2,fill = True,color = 'k'))
    patch.append(plt.Rectangle(xy = (4,4),width = 2,height = 2,fill = True,color = 'k'))
    
    def init():
        for i in range(9):
            patch[i].set_color('k')
            ax.add_patch(patch[i])
        return patch
    
    def animate(i):
        value = (np.array(Color[i]) == 0)
        for j in range(9):
            patch[j].set_color('k' if value[j] else 'r')
    
        return patch
    
    anim = animation.FuncAnimation(fig, animate,  
                                    init_func=init, 
                                    frames=3, 
                                    interval=1000,
                                    blit=True)
    
    plt.show()