Skip to content

Commit 6c0c30b

Browse files
committed
Simplify implementation of Text3D.
This avoids saving a second copy of the whole position, and the inconsistencies that might arise thereof.
1 parent 3f92f43 commit 6c0c30b

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
``mplot3d.art3d.get_dir_vector`` always returns NumPy arrays
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
For consistency, `~.mplot3d.art3d.get_dir_vector` now always returns NumPy
5+
arrays, even if the input is a 3-element iterable.

lib/mpl_toolkits/mplot3d/art3d.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import numpy as np
1313

1414
from matplotlib import (
15-
artist, colors as mcolors, lines, text as mtext, path as mpath)
15+
artist, cbook, colors as mcolors, lines, text as mtext, path as mpath)
1616
from matplotlib.collections import (
1717
LineCollection, PolyCollection, PatchCollection, PathCollection)
1818
from matplotlib.colors import Normalize
@@ -49,13 +49,12 @@ def get_dir_vector(zdir):
4949
- 'y': equivalent to (0, 1, 0)
5050
- 'z': equivalent to (0, 0, 1)
5151
- *None*: equivalent to (0, 0, 0)
52-
- an iterable (x, y, z) is returned unchanged.
52+
- an iterable (x, y, z) is converted to a NumPy array, if not already
5353
5454
Returns
5555
-------
5656
x, y, z : array-like
57-
The direction vector. This is either a numpy.array or *zdir* itself if
58-
*zdir* is already a length-3 iterable.
57+
The direction vector.
5958
"""
6059
if zdir == 'x':
6160
return np.array((1, 0, 0))
@@ -66,7 +65,7 @@ def get_dir_vector(zdir):
6665
elif zdir is None:
6766
return np.array((0, 0, 0))
6867
elif np.iterable(zdir) and len(zdir) == 3:
69-
return zdir
68+
return np.array(zdir)
7069
else:
7170
raise ValueError("'x', 'y', 'z', None or vector of length 3 expected")
7271

@@ -96,21 +95,22 @@ def __init__(self, x=0, y=0, z=0, text='', zdir='z', **kwargs):
9695
self.set_3d_properties(z, zdir)
9796

9897
def set_3d_properties(self, z=0, zdir='z'):
99-
x, y = self.get_position()
100-
self._position3d = np.array((x, y, z))
98+
self._z = z
10199
self._dir_vec = get_dir_vector(zdir)
102100
self.stale = True
103101

104102
@artist.allow_rasterization
105103
def draw(self, renderer):
104+
position3d = np.array((self._x, self._y, self._z))
106105
proj = proj3d.proj_trans_points(
107-
[self._position3d, self._position3d + self._dir_vec], renderer.M)
106+
[position3d, position3d + self._dir_vec],
107+
renderer.M)
108108
dx = proj[0][1] - proj[0][0]
109109
dy = proj[1][1] - proj[1][0]
110110
angle = math.degrees(math.atan2(dy, dx))
111-
self.set_position((proj[0][0], proj[1][0]))
112-
self.set_rotation(_norm_text_angle(angle))
113-
mtext.Text.draw(self, renderer)
111+
with cbook._setattr_cm(self, _x=proj[0][0], _y=proj[1][0],
112+
_rotation=_norm_text_angle(angle)):
113+
mtext.Text.draw(self, renderer)
114114
self.stale = False
115115

116116
def get_tightbbox(self, renderer):

0 commit comments

Comments
 (0)