如何用matplotlib制作序列数组的动画?

2024-10-01 15:47:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我是python的初学者。我的问题是两辆车必须穿过一个简单的十字路口。我已经用一种叫做FIACRE的语言得到了我需要的答案。在这种语言中,我实现了一个6 x 6的网格,如下所示:

a= [[1,1,1,1,1,1], 
    [1,1,0,2,1,1],
    [1,3,0,0,0,1],
    [1,0,0,0,0,1],
    [1,1,0,0,1,1],
    [1,1,1,1,1,1]]

其中1代表人行道,0代表街道,2和3代表汽车。我得到了这样一个结果列表:m1,m2,m3,m4,m5和m6。每一个都代表着一种运动。在

^{pr2}$

我已经开始实现一个python代码:

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

# here I import the .txt file with the m steps obtained in FIACRE
steps = pd.read_table("C:\\Users\\...\\ file.txt")

nrows, ncols = 6,6 #number of rows and columns

# Make a 6x6 grid...
image = np.array([[1,1,1,1,1,1],
                  [1,1,0,3,1,1],
                  [1,2,0,0,0,1],
                  [1,0,0,0,0,1],
                  [1,1,0,0,1,1],
                  [1,1,1,1,1,1]])

plt.matshow(image)
plt.show()

我期望的是一个代码,它实现了更改数组(映像)所需的步骤。然后,对每个步骤中获得的数组进行动画模拟汽车运动,并保存在gif中。在


Tags: the代码imageimporttxt语言asnp

热门问题