2016-03-03 393 views
1

我想繪製來自CSV文件的車輛的3D軌跡,繪圖很容易,我想製作動畫,實際上是動作的「重放」。我根據我的代碼從這個例子(http://matplotlib.org/examples/animation/simple_3danim.html),然後只將它修改爲只圖一線和讀取一個CSV文件中的數據通過大熊貓被讀取,代碼如下所示:使用matplotlib從CSV數據繪製3D軌跡

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

我打印ž只是爲了看看數據是否被正確地重複,但我得到的是一個白色的情節是這樣的:

Plot showing absolutely nothing.

+0

如果有人想看到數據:https://gist.github.com/alduxvm/4309265d19c6525244a5 – Aldux

回答

0

至少有兩個問題與您的代碼:

  1. 如何數據的方式是建立
  2. 長度每秒

這裏幀被修改的工作示例,請看一看可變 數據是如何安排的:

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

可以觀看that flight只是被追蹤的美麗