Объект Arrow3D не имеет атрибута do_3d_projection; настроить оси X, Y, Z на 3D-плоскостиPython

Программы на Python
Ответить
Anonymous
 Объект Arrow3D не имеет атрибута do_3d_projection; настроить оси X, Y, Z на 3D-плоскости

Сообщение Anonymous »

Цель кода — использовать стрелку3D для запуска осей X, Y, Z на трехмерной плоскости.
Параметрыarrow3D: x, y, z, которые по умолчанию равны нулю; и dx, dy, dz — длина осей X, Y, Z. Таким образом, я мог бы настроить свое окно на трехмерной плоскости.
Итак, в приведенном ниже коде стрелка3D наследует FancyArrowPatch и имеет ее атрибуты _xyz и _dxdydz.

Код: Выделить всё

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.proj3d import proj_transform
from mpl_toolkits.mplot3d.axes3d import Axes3D
from matplotlib.text import Annotation

from matplotlib.patches import FancyArrowPatch

# X axis, Y axis, and Z axis should be added into axes like other artists
# class Arrow3D is created for them
# Arrow3D inherits FancyArrowPatch, since those axes are like arrows
class Arrow3D(FancyArrowPatch):
def __init__(self, x, y, z, dx, dy, dz, *args, **kwargs):
super().__init__((0,0), (0,0), *args, **kwargs)
self._xyz = (x,y,z)
self._dxdydz = (dx,dy,dz)

def draw(self, renderer):
x1,y1,z1 = self._xyz
dx,dy,dz = self._dxdydz
x2,y2,z2 = (x1+dx,y1+dy,z1+dz)

xs, ys, zs = proj_transform((x1,x2),(y1,y2),(z1,z2), renderer.M)
self.set_positions((xs[0],ys[0]),(xs[1],ys[1]))
super().draw(renderer)

def _arrow3D(ax, x, y, z, dx, dy, dz, *args, **kwargs):
'''Add an 3d arrow to an `Axes3D` instance.'''

arrow = Arrow3D(x, y, z, dx, dy, dz, *args, **kwargs)
ax.add_artist(arrow)

# an object of class Axes3D will have an attribute-'arrow3D', which is the function _arrow3D
# this object could be axes, when it is created by passing the projection='3d' keyword argument to Figure.add_subplot
setattr(Axes3D,'arrow3D',_arrow3D)

# an object of class Axes3D will have an attribute-'arrow3D', which is the function _arrow3D
# this object could be axes, when it is created by passing the projection='3d' keyword argument to Figure.add_subplot
setattr(Axes3D,'arrow3D',_arrow3D)

class Annotation3D(Annotation):
def __init__(self, text, xyz, *args, **kwargs):
super().__init__(text, xy=(0,0), *args, **kwargs)
self._xyz = xyz

def draw(self, renderer):
x2, y2, z2 = proj_transform(*self._xyz, renderer.M)
self.xy=(x2,y2)
super().draw(renderer)

def _annotate3D(ax,text, xyz, *args, **kwargs):
'''Add anotation `text` to an `Axes3d` instance.'''

annotation= Annotation3D(text, xyz, *args, **kwargs)
ax.add_artist(annotation)

setattr(Axes3D,'annotate3D',_annotate3D)

Определив стрелку3D, я затем определяю _axesSetup, чтобы можно было установить мой 3D-фон.

Код: Выделить всё

import arrow3d
from mpl_toolkits.mplot3d.axes3d import Axes3D
from matplotlib.text import Annotation
from matplotlib.patches import FancyArrowPatch
import numpy as np

def _axesSetup(ax, lX, uX, lY, uY, lZ, uZ):
# set xlimit, draw x axis, annotate x axis
# x axis
ax.set_xlim(lX, uX)
ax.arrow3D(lX, 0, 0, uX - lX, 0, 0, mutation_scale=15, arrowstyle="-|>",  color='red')
ax.annotate3D('X', (uX, 0, 0), xytext=(0, 0), textcoords=('offset points'), color = 'xkcd:eggplant', weight="bold")

# y axis
ax.set_ylim(lY, uY)
ax.arrow3D(0, lY, 0, 0, uY - lY, 0, mutation_scale=15, arrowstyle="-|>",  color='red')
ax.annotate3D('Y', (0, uY, 0), xytext=(0, 0), textcoords=('offset points'), color = 'xkcd:eggplant', weight="bold")

# z axis
ax.set_zlim(lZ, uZ)
ax.arrow3D(0, 0, lZ, 0, 0, uZ - lZ, mutation_scale=15, arrowstyle="-|>",  color='red')
ax.annotate3D('Z', (0, 0, uZ), xytext=(0, 0), textcoords=('offset points'), color = 'xkcd:eggplant', weight="bold")

setattr(Axes3D, 'axesSetup', _axesSetup )
# setattr create 'axesSetup' attribute for class Axes3D if 'axesSetup' is not found
# an object of class Axes3D is the axes projected in 3D

Раньше работало, но сейчас ничего не показывает. В другом сообщении уже выяснилось, что это связано с обновлением Matplotlib. Может ли кто-нибудь помочь мне изменить мой код, чтобы он работал?

Подробнее здесь: https://stackoverflow.com/questions/792 ... axis-on-3d
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Python»