8000 Finall improvements to plot extent calculation. · cirosantilli/matplotlib@dfbe69c · GitHub
[go: up one dir, main page]

Skip to content

Commit dfbe69c

Browse files
Phil Elsonpelson
authored andcommitted
Finall improvements to plot extent calculation.
1 parent 3ccff6f commit dfbe69c

File tree

4 files changed

+47
-24
lines changed

4 files changed

+47
-24
lines changed

lib/matplotlib/axes.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1478,17 +1478,22 @@ def _update_line_limits(self, line):
14781478
data_path = path
14791479

14801480
elif line_trans.contains_branch(self.transData):
1481-
# transform the path all the way down (to device coordinates)
1482-
# then come back up (by inverting transData) to data coordinates.
1483-
# it would be possible to do this by identifying the transform
1484-
# needed to go from line_trans directly to transData, doing it
1485-
# this way means that the line instance has an opportunity to
1486-
# cache the transformed path.
1487-
device2data = self.transData.inverted()
1488-
data_path = device2data.transform_path(line.get_transformed_path())
1489-
else:
1490-
# for backwards compatibility we update the dataLim with the
1491-
# coordinate range of the given path, even though the coordinate
1481+
# identify the transform to go from line's coordinates
1482+
# to data coordinates
1483+
trans_to_data = line_trans - self.transData
1484+
1485+
# if transData is affine we can use the cached non-affine component
1486+
# of line's path. (since the non-affine part of line_trans is
1487+
# entirely encapsulated in trans_to_data).
1488+
if self.transData.is_affine:
1489+
line_trans_path = line._get_transformed_path()
1490+
na_path, _ = line_trans_path.get_transformed_path_and_affine()
1491+
data_path = trans_to_data.transform_path_affine(na_path)
1492+
else:
1493+
data_path = trans_to_data.transform_path(path)
1494+
else:
1495+
# for backwards compatibility we update the dataLim with the
1496+
# coordinate range of the given path, even though the coordinate
14921497
# systems are completely different. This may occur in situations
14931498
# such as when ax.transAxes is passed through for absolute
14941499
# positioning.

lib/matplotlib/lines.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,7 @@ def contains(self, mouseevent):
249249
if len(self._xy)==0: return False,{}
250250

251251
# Convert points to pixels
252-
if self._transformed_path is None:
253-
self._transform_path()
254-
path, affine = self._transformed_path.get_transformed_path_and_affine()
252+
path, affine = self._get_transformed_path().get_transformed_path_and_affine()
255253
path = affine.transform_path(path)
256254
xy = path.vertices
257255
xt = xy[:, 0]
@@ -457,11 +455,14 @@ def _transform_path(self, subslice=None):
457455
_path = self._path
458456
self._transformed_path = TransformedPath(_path, self.get_transform())
459457

460-
def get_transformed_path(self):
461-
"""Return the path of this line, (fully) transformed using the line's transform."""
458+
def _get_transformed_path(self):
459+
"""
460+
Return the :class:`~matplotlib.transforms.TransformedPath` instance
461+
of this line.
462+
"""
462463
if self._transformed_path is None:
463464
self._transform_path()
464-
return self._transformed_path.get_fully_transformed_path()
465+
return self._transformed_path
465466

466467
def set_transform(self, t):
467468
"""
@@ -491,8 +492,8 @@ def draw(self, renderer):
491492
subslice = slice(max(i0-1, 0), i1+1)
492493
self.ind_offset = subslice.start
493494
self._transform_path(subslice)
494-
if self._transformed_path is None:
495-
self._transform_path()
495+
496+
transformed_path = self._get_transformed_path()
496497

497498
if not self.get_visible(): return
498499

@@ -516,7 +517,7 @@ def draw(self, renderer):
516517

517518
funcname = self._lineStyles.get(self._linestyle, '_draw_nothing')
518519
if funcname != '_draw_nothing':
519-
tpath, affine = self._transformed_path.get_transformed_path_and_affine()
520+
tpath, affine = transformed_path.get_transformed_path_and_affine()
520521
if len(tpath.vertices):
521522
self._lineFunc = getattr(self, funcname)
522523
funcname = self.drawStyles.get(self._drawstyle, '_draw_lines')
@@ -537,7 +538,7 @@ def draw(self, renderer):
537538
gc.set_linewidth(self._markeredgewidth)
538539
gc.set_alpha(self._alpha)
539540
marker = self._marker
540-
tpath, affine = self._transformed_path.get_transformed_points_and_affine()
541+
tpath, affine = transformed_path.get_transformed_points_and_affine()
541542
if len(tpath.vertices):
542543
# subsample the markers if markevery is not None
543544
markevery = self.get_markevery()

lib/matplotlib/tests/test_transforms.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ def test_transform_shortcuts(self):
248248
self.assertEqual(self.stack1 - self.ta3, self.ta1 + (self.tn1 + self.ta2))
249249
self.assertEqual(self.stack2 - self.ta3, self.ta1 + self.tn1 + self.ta2)
250250

251+
self.assertEqual((self.ta2 + self.ta3) - self.ta3 + self.ta3, self.ta2 + self.ta3)
252+
251253
def test_contains_branch(self):
252254
r1 = (self.ta2 + self.ta1)
253255
r2 = (self.ta2 + self.ta1)
@@ -346,10 +348,24 @@ def test_pathc_extents_affine(self):
346348
patch = mpatches.PathPatch(pth, transform=offset + ax.transData)
347349
ax.add_patch(patch)
348350
expeted_data_lim = np.array([[0., 0.], [10., 10.]]) + 10
351+
np.testing.assert_array_almost_equal(ax.dataLim.get_points(),
352+
expeted_data_lim)
353+
354+
355+
def test_line_extents_for_non_affine_transData(self):
356+
ax = plt.axes(projection='polar')
357+
# add 10 to the radius of the data
358+
offset = mtrans.Affine2D().translate(0, 10)
359+
360+
plt.plot(range(10), transform=offset + ax.transData)
361+
# the data lim of a polar plot is stored in coordinates
362+
# before a transData transformation, hence the data limits
363+
# are not what is being shown on the actual plot.
364+
expeted_data_lim = np.array([[0., 0.], [9., 9.]]) + [0, 10]
349365
np.testing.assert_array_almost_equal(ax.dataLim.get_points(),
350366
expeted_data_lim)
351367

352368

353369
if __name__=='__main__':
354370
import nose
355-
nose.runmodule(argv=['-s','--with-doctest'], exit=False)
371+
nose.runmodule(argv=['-s','--with-doctest'], exit=False)

lib/matplotlib/transforms.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,7 +1040,7 @@ class Transform(TransformNode):
10401040
- :meth:`transform`
10411041
- :attr:`is_separable`
10421042
- :attr:`has_inverse`
1043-
- :meth:`inverted` (if :meth:`has_inverse` can return True)
1043+
- :meth:`inverted` (if :attr:`has_inverse` is True)
10441044
10451045
If the transform needs to do something non-standard with
10461046
:class:`matplotlib.path.Path` objects, such as adding curves
@@ -1141,6 +1141,8 @@ def __sub__(self, other):
11411141
# similarly, when B contains tree A, we can avoid decending A at all, basically:
11421142
A - B == (B - A).inverted() or B-1
11431143
1144+
For clarity, the result of ``(A + B) - B + B == (A + B)``.
1145+
11441146
"""
11451147
# we only know how to do this operation if other is a Transform.
11461148
if not isinstance(other, Transform):
@@ -1164,7 +1166,6 @@ def __sub__(self, other):
11641166
raise ValueError('It is not possible to compute transA - transB '
11651167
'since transB cannot be inverted and there is no '
11661168
'shortcut possible.')
1167-
>>>>>>> Several bugs fixed, particularly with Polar & Geo.
11681169

11691170
def __array__(self, *args, **kwargs):
11701171
"""

0 commit comments

Comments
 (0)
0