小编Fra*_*nTo的帖子

在Python中绘制列表中的数据

我需要绘制一些物体(汽车)的速度。

每个速度都通过例程计算并写入文件中,大致如下(我删除了一些行以简化):

thefile_v= open('vels.txt','w') 

for car in cars:
    velocities.append(new_velocity) 

     if len(car.velocities) > 4:
          try:
              thefile_v.write("%s\n" %car.velocities) #write vels once we get 5 values
              thefile_v.close

          except:
              print "Unexpected error:", sys.exc_info()[0]
              raise
Run Code Online (Sandbox Code Playgroud)

其结果是一个文本文件,其中包含每辆车的速度列表。

像这样的东西:

[0.0, 3.8, 4.5, 4.3, 2.1, 2.2, 0.0]
[0.0, 2.8, 4.0, 4.2, 2.2, 2.1, 0.0]
[0.0, 1.8, 4.2, 4.1, 2.3, 2.2, 0.0]
[0.0, 3.8, 4.4, 4.2, 2.4, 2.4, 0.0]
Run Code Online (Sandbox Code Playgroud)

然后我想绘制每个速度

with open('vels.txt') as f:
    lst = [line.rstrip() for line in f]

plt.plot(lst[1]) #lets plot the second line
plt.show()
Run Code Online (Sandbox Code Playgroud)

这就是我发现的。这些值被视为字符串并将它们作为 …

python arrays string list matplotlib

4
推荐指数
1
解决办法
3万
查看次数

标签 统计

arrays ×1

list ×1

matplotlib ×1

python ×1

string ×1