简体   繁体   English

使用matplotlib从CSV数据绘制3D轨迹

[英]Plotting 3D trajectory from CSV data using matplotlib

I'm trying to plot a 3D trajectory of a vehicle that comes from a CSV file, plotting is easy, I want to make the animation, actually a "replay" of the movements. 我正在尝试绘制来自CSV文件的车辆的3D轨迹,绘制很容易,我想制作动画,实际上是动作的“重播”。 I based my code from this example ( http://matplotlib.org/examples/animation/simple_3danim.html ) and then just modify it to only plot one line and to read the data from a CSV file being read by pandas, the code looks like this: 我根据此示例创建了代码( http://matplotlib.org/examples/animation/simple_3danim.html ),然后对其进行修改,使其仅绘制一条线并从熊猫读取的CSV文件中读取数据,该代码看起来像这样:

import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d.axes3d as p3
import matplotlib.animation as animation
import pandas as pd

def update_lines(num, data, line):
    # NOTE: there is no .set_data() for 3 dim data...
    x = data['x'].values[num]
    y = data['y'].values[num]
    z = data['z'].values[num]
    line[0].set_data(x,y)
    line[0].set_3d_properties(z)
    print z
    return line

# Attaching 3D axis to the figure
fig = plt.figure()
ax = p3.Axes3D(fig)

# Reading the data from a CSV file using pandas
data = pd.read_csv('data.csv',sep=',',header=0)

# Creating fifty line objects.
# NOTE: Can't pass empty arrays into 3d version of plot()
x = np.array([0])
y = np.array([0])
z = np.array([0])

line = ax.plot(x, y, z)

# Setting the axes properties
ax.set_xlim3d([0.0, 3.0])
ax.set_xlabel('X')

ax.set_ylim3d([0.0, 3.0])
ax.set_ylabel('Y')

ax.set_zlim3d([0.0, 2.0])
ax.set_zlabel('Z')

ax.set_title('3D Test')

# Creating the Animation object
line_ani = animation.FuncAnimation(fig, update_lines, len(data), fargs=(data, line),
                                   interval=10, blit=False)

plt.show()

I print the z just to see if the data is being iterated correctly, but all I get is a white plot like this: 我打印z只是为了查看数据是否正确迭代,但是我得到的只是一个白色图,如下所示:

Plot showing absolutely nothing. 情节什么都没显示。

at least, there are two issues with your code: 至少,您的代码有两个问题:

  1. the way of how data is build 数据构建方式
  2. length of frames per second 每秒帧数

here is the modified working example, please take a look how data variable was arranged: 这是修改后的工作示例,请查看数据变量的排列方式:

import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d.axes3d as p3
import matplotlib.animation as animation
import pandas as pd
from sys import exit
def update_lines(num, data, line):
    # NOTE: there is no .set_data() for 3 dim data...
    line.set_data(data[0:2, :num])    
    line.set_3d_properties(data[2, :num])    
    return line

# Attaching 3D axis to the figure
fig = plt.figure()
ax = p3.Axes3D(fig)

# Reading the data from a CSV file using pandas
repo = pd.read_csv('data.csv',sep=',',header=0)
data = np.array((repo['x'].values, repo['y'].values, repo['z'].values))
print data.shape[1]
#exit()

# Creating fifty line objects.
# NOTE: Can't pass empty arrays into 3d version of plot()
line = ax.plot(data[0, 0:1], data[1, 0:1], data[2, 0:1])[0]

# Setting the axes properties
ax.set_xlim3d([-2.0, 2.0])
ax.set_xlabel('X')

ax.set_ylim3d([-2.0, 2.0])
ax.set_ylabel('Y')

ax.set_zlim3d([-2.0, 2.0])
ax.set_zlabel('Z')

ax.set_title('3D Test')

# Creating the Animation object
line_ani = animation.FuncAnimation(fig, update_lines, data.shape[1], fargs=(data, line), interval=50, blit=False)

plt.show()

you can watch the beauty of that flight just being tracked 您可以看到刚刚被跟踪的那段飞行的美丽

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM