如何使用读取x和y位置的值来绘制轨迹?



我试图通过读取包含3列数据的文件来绘制弹跳球的轨迹,例如,t, x和y。t是时间,x和y分别是球的位置。我在数据中读取每个变量的列表。我对python有点陌生,所以我不知道我做错了什么。我试着把x和y的值插入到列表中。我在"plt上得到一个语法错误。情节(t, 0)";线。

import matplotlib.pyplot as plt
import math as math
filename = 'NewTrackerVid_.txt'
infile = open(filename, 'r') # Open file for reading
line = infile.readline() # Read first line
# Read x and y coordinates from the file and store in lists
t = []
x = []
y = []
for line in infile:
words = line.split() # Split line into words
t.append(float(words[0]))
x.append(float(words[1]))
y.append(float(words[2]))
infile.close()
θ = []
for i in range(len(x)):
θ.append(float(math.tan(y[i] / x[i])
plt.plot(t, θ)
plt.xlabel('t')
plt.ylabel('θ')
plt.title("Trajectory vs Time")

我想你要做的是对所有x,y绘制tan(y[I]/x[I])。Numpy和熊猫会帮上大忙的!

import numpy as np
import pandas as pd
df=pd.read_csv(my_file_name.txt,columns=['t','x','y'])
df['theta']=np.tan((df['x']/df['y']))
df['theta'].plot()

最新更新