'FancyArrow3D' object has no attribute 'do_3d_projection'问题

《程序员数学:用Python学透线性代数和微积分》第3章,源码bug修正。
错误原因:
画“三维箭头”图形的时候,系统报错“AttributeError: ‘Arrow3D’ object has no attribute ‘do_3d_projection’”。
猜测可能是matplotlib3.5(含)以后版本与书中源代码不兼容的问题。
 
修正方式:
增加引入
import numpy as np

 

 
FancyArrow3D类修改方法:
    def __init__(self, xs, ys, zs, *args, **kwargs):
        # FancyArrowPatch.__init__(self, (0,0), (0,0), *args, **kwargs)
        super().__init__((0,0), (0,0), *args, **kwargs)
        self._verts3d = xs, ys, zs

 

 
FancyArrow3D类增加方法
    def do_3d_projection(self, renderer=None):
        xs3d, ys3d, zs3d = self._verts3d
        xs, ys, zs = proj3d.proj_transform(xs3d, ys3d, zs3d, self.axes.M)
        self.set_positions((xs[0], ys[0]), (xs[1], ys[1]))
        return np.min(zs)

 

 
FancyArrow3D类修改方法:
    def draw(self, renderer):
        xs3d, ys3d, zs3d = self._verts3d
        # xs, ys, zs = proj3d.proj_transform(xs3d, ys3d, zs3d, renderer.M)
        xs, ys, zs = proj3d.proj_transform(xs3d, ys3d, zs3d, self.axes.M)
        self.set_positions((xs[0],ys[0]),(xs[1],ys[1]))
        FancyArrowPatch.draw(self, renderer)

 

 
修改draw3d方法:
def draw3d(*objects, origin=True, axes=True, width=6, save_as=None, azim=None, elev=None, xlim=None, ylim=None, zlim=None, xticks=None, yticks=None, zticks=None,depthshade=False):
 
    前边代码忽略
 
        elif type(object) == Arrow3D:
            xs, ys, zs = zip(object.tail, object.tip)
            # a = FancyArrow3D(xs,ys,zs, mutation_scale=20,arrowstyle='-|>', color=object.color)
            # ax.add_artist(a)
            arrow_prop_dict = dict(mutation_scale=20, arrowstyle='-|>', color='k', shrinkA=0, shrinkB=0)
            a = FancyArrow3D(xs, ys, zs, **arrow_prop_dict)
            ax.add_artist(a)
 
    后边代码忽略
 

 

 
 

posted on 2023-10-10 14:38  王培  阅读(618)  评论(0编辑  收藏  举报

导航