我必须在情节上创建一个钟摆模拟。我必须从文件中读取数据,然后我唯一需要解决的是 X 和 Y 线,我做了什么。指定我应该使用“roll”/“pitch”/“yaw”列,并使用其中一个进行模拟。有文件:https ://pastebin.com/D2Tt3tBj 这是我的代码:
import pandas as pd
import tkinter as tk
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from mpl_toolkits.mplot3d import Axes3D
from tkinter import ttk
from tkinter import scrolledtext
import numpy as np
import matplotlib.animation as animation
from matplotlib.animation import FuncAnimation
pd.set_option('display.max_rows', None) # wyswietla caly plik
def odrzuc_n(df_full, n):
df_full = df_full.iloc[n:]
return df_full
def wczytaj_dane():
data = pd.read_csv("outputPendulum02.log", delim_whitespace=True)
data.columns = ["roll", "pitch", "yaw", "a_x", "a_y", "a_z", "m_x", "m_y", "m_z", "omega_x", "omega_y", "omega_z"]
data = data.drop(columns=["a_x", "a_y", "a_z", "m_x", "m_y", "m_z", "omega_x", "omega_y", "omega_z"])
data.index = [x for x in range(1, len(data.values) + 1)]
data.index.name = 'id'
df = pd.DataFrame(data)
seconds = []
x_cord = []
y_cord = []
which_angle = 'pitch'
for (index_label, row_series) in df.iterrows():
second = index_label * 0.04
seconds.append(second)
x = 50 * np.sin(row_series[which_angle])
y = 50 - 50 * np.cos(row_series[which_angle])
x_cord.append(x)
y_cord.append(y)
df['seconds'] = seconds
df['x_cord'] = x_cord
df['y_cord'] = y_cord
# print(df)
return df
df_full = wczytaj_dane()
how_many_to_delete = 500
df_full = odrzuc_n(df_full, how_many_to_delete)
data_cut = df_full
fig, ax = plt.subplots()
xdata, ydata = [], []
ln, = plt.plot([], [], 'ro')
def init(): # only required for blitting to give a clean slate.
ax.set_xlim(df_full['x_cord'].min()-5, df_full['x_cord'].max()+5)
ax.set_ylim(df_full['y_cord'].min()-5, df_full['y_cord'].max()+5)
return ln,
def update(frame):
xdata = (df_full.iloc[frame, 4])
ydata = (df_full.iloc[frame, 5])
ln.set_data(xdata, ydata)
if frame > 1225:
return 0
return ln,
ani = FuncAnimation(fig, update, frames=range(how_many_to_delete, 1225),
init_func=init, blit=True)
plt.show()
有点乱……“odrzuc_n”函数是从数据帧中删除 N 行的函数。“Wczytaj_dane”正在从文件中读取数据。文件的列在代码中描述。最后是我的问题。它出什么问题了?它在动画结束时显示错误。我真的不知道 X 和 Y 是否正确确定。我必须在 GUI 中制作它,这就是我引入 tkinter 等的原因。但这部分代码只生成情节。