robotics-toolbox-python运行plot函数报'Axes3D'无w_xaxis属性的解决问询
解决robotics-toolbox-python中plot方法的AttributeError错误
问题描述
运行以下robotics-toolbox-python代码时,最后两行触发AttributeError:
import roboticstoolbox as rtb robot = rtb.models.Panda() print("robot = ", robot) Te = robot.fkine(robot.qr) # forward kinematics print("Te = ", Te) from spatialmath import SE3 Tep = SE3.Trans(0.6, -0.3, 0.1) * SE3.OA([0, 1, 0], [0, 0, -1]) sol = robot.ik_LM(Tep) # solve IK print("sol=", sol) q_pickup = sol[0] print(robot.fkine(q_pickup)) # FK shows that desired end-effector pose was achieved qt = rtb.jtraj(robot.qr, q_pickup, 50) robot.plot(qt.q, backend='pyplot', movie='panda1.gif')
错误信息
Traceback (most recent call last): File "f:\Codefield\CODE_P\youtube_follow\test.py", line 17, in <module> robot.plot(qt.q, backend='pyplot', movie='panda1.gif') File "G:\Conda_envlist\RV\lib\site-packages\roboticstoolbox\robot\BaseRobot.py", line 2372, in plot images.append(env.getframe()) File "G:\Conda_envlist\RV\lib\site-packages\roboticstoolbox\backends\PyPlot\PyPlot.py", line 419, in getframe self.ax.w_xaxis.set_pane_color((1.0, 1.0, 1.0, 1.0)) AttributeError: 'Axes3D' object has no attribute 'w_xaxis'. Did you mean: 'xaxis'?
解决方案
这个错误是因为matplotlib 3.7+版本修改了Axes3D的轴属性命名,原有的w_xaxis/w_yaxis/w_zaxis被移除,替换为xaxis/yaxis/zaxis,而robotics-toolbox-python的pyplot后端暂未适配该变更。以下是三种解决方式:
方式一:降级matplotlib到兼容版本
安装matplotlib 3.6.x版本(该版本保留了旧属性命名):
pip install matplotlib==3.6.3 --force-reinstall
方式二:手动修改robotics-toolbox的PyPlot后端代码
找到你的环境中PyPlot.py文件的路径(示例路径:G:\Conda_envlist\RV\lib\site-packages\roboticstoolbox\backends\PyPlot\PyPlot.py),修改以下内容:
- 将所有
self.ax.w_xaxis替换为self.ax.xaxis - 将所有
self.ax.w_yaxis替换为self.ax.yaxis - 将所有
self.ax.w_zaxis替换为self.ax.zaxis
保存文件后重新运行代码即可。
方式三:切换到其他绘图后端
放弃使用pyplot后端,改用qt或vpython后端,修改plot代码行:
# 改用qt后端 robot.plot(qt.q, backend='qt', movie='panda1.gif') # 或改用vpython后端 robot.plot(qt.q, backend='vpython', movie='panda1.gif')
内容的提问来源于stack exchange,提问作者Jifang On
相关产品推荐
相关产品推荐

