import os.path import matplotlib.pyplot as plt import pandas from matplotlib.widgets import Slider from common import clibs def initialization(): path, curves = None, None try: path = clibs.data_dd["path"] curves = clibs.data_dd["curves"] except Exception: clibs.w2t("程序未开始运行,暂无数据可以展示......\n", "red") return None, None for curve in curves: if not os.path.exists(f"{path}/{curve}.csv"): clibs.w2t(f"{curve}曲线数据暂未生成,稍后再试......\n", "orange") return None, None return path, curves def data_plot(path, curve): titles = {"hw_joint_vel_feedback": "各关节最大速度曲线", "device_servo_trq_feedback": "各关节平均有效转矩曲线"} ylabels = {"hw_joint_vel_feedback": "速度(rad/s)", "device_servo_trq_feedback": "转矩(Nm)"} fig, axes = plt.subplots(figsize=(10, 4.5), dpi=100) cols = [f"{curve}_{i}" for i in range(6)] cols.insert(0, "time") df = pandas.read_csv(f"{path}/{curve}.csv") plt.plot(df[cols[1]], label="一轴") plt.plot(df[cols[2]], label="二轴") plt.plot(df[cols[3]], label="三轴") plt.plot(df[cols[4]], label="四轴") plt.plot(df[cols[5]], label="五轴") plt.plot(df[cols[6]], label="六轴") axes.set_title(titles[curve]) axes.set_ylabel(ylabels[curve]) axes.legend(loc="upper right") slider_position = plt.axes((0.1, 0.01, 0.8, 0.05), facecolor="blue") # (left, bottom, width, height) scrollbar = Slider(slider_position, 'Time', 1, int(len(df)), valstep=1) def update(val): pos = scrollbar.val axes.set_xlim([pos, pos + 10]) fig.canvas.draw_idle() scrollbar.on_changed(update) fig.tight_layout(rect=(0, 0.02, 0.96, 1)) # tuple (left, bottom, right, top) def main(): path, curves = initialization() if not path or not curves: return for curve in curves: data_plot(path, curve) plt.rcParams['font.sans-serif'] = ['SimHei'] plt.rcParams['axes.unicode_minus'] = False plt.rcParams['figure.dpi'] = 100 plt.rcParams['font.size'] = 14 plt.rcParams['lines.marker'] = 'o' plt.rcParams["figure.autolayout"] = True plt.show() # threads = [threading.Thread(target=data_plot, args=(path, curve)) for curve in curves] # for t in threads: # t.daemon = True # t.start() # for curve in curves: # t = threading.Thread(target=data_plot, args=(path, curve)) # t.daemon = True # t.start() if __name__ == '__main__': main()