Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 02a94c9

Browse files
get_offsets3d and set_offsets3d
1 parent fd5f164 commit 02a94c9

File tree

2 files changed

+115
-7
lines changed

2 files changed

+115
-7
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
3D Collections have `set_offset3d` and `get_offset3d` methods
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
All 3D Collections (`Patch3DCollection`, `Path3DCollection`,
5+
`Poly3DCollection`) now have `set_offset3d` and `get_offset3d` methods
6+
which allow you to set and get the offset of the collection in data
7+
coordinates. In other words, this allows you to set and get the position of the
8+
of the collection points.

lib/mpl_toolkits/mplot3d/art3d.py

Lines changed: 107 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -598,19 +598,20 @@ def set_3d_properties(self, zs, zdir):
598598
# Force the collection to initialize the face and edgecolors
599599
# just in case it is a scalarmappable with a colormap.
600600
self.update_scalarmappable()
601-
offsets = self.get_offsets()
601+
offsets = super().get_offsets()
602602
if len(offsets) > 0:
603603
xs, ys = offsets.T
604604
else:
605605
xs = []
606606
ys = []
607-
self._offsets3d = juggle_axes(xs, ys, np.atleast_1d(zs), zdir)
607+
self._zdir = zdir
608+
self.set_offsets3d(np.ma.column_stack((xs, ys, np.atleast_1d(zs))), zdir)
608609
self._z_markers_idx = slice(-1)
609610
self._vzs = None
610611
self.stale = True
611612

612613
def do_3d_projection(self):
613-
xs, ys, zs = self._offsets3d
614+
xs, ys, zs = self.get_offsets3d()
614615
vxs, vys, vzs, vis = proj3d.proj_transform_clip(xs, ys, zs,
615616
self.axes.M)
616617
self._vzs = vzs
@@ -642,6 +643,30 @@ def get_edgecolor(self):
642643
return self.get_facecolor()
643644
return self._maybe_depth_shade_and_sort_colors(super().get_edgecolor())
644645

646+
def set_offsets3d(self, offsets, zdir='z'):
647+
"""
648+
Set the 3d offsets for the collection.
649+
650+
Parameters
651+
----------
652+
offsets : (N, 3) or (3,) array-like
653+
The offsets to be set.
654+
zdir : {'x', 'y', 'z'}
655+
The axis in which to place the offsets. Default: 'z'.
656+
See `.get_dir_vector` for a description of the values.
657+
"""
658+
return _set_offsets3d(self, offsets, zdir)
659+
660+
def get_offsets3d(self):
661+
"""Return the 3d offsets for the collection.
662+
663+
Returns
664+
-------
665+
offsets : (N, 3) array
666+
The offsets for the collection.
667+
"""
668+
return _get_offsets3d(self)
669+
645670

646671
class Path3DCollection(PathCollection):
647672
"""
@@ -696,13 +721,13 @@ def set_3d_properties(self, zs, zdir):
696721
# Force the collection to initialize the face and edgecolors
697722
# just in case it is a scalarmappable with a colormap.
698723
self.update_scalarmappable()
699-
offsets = self.get_offsets()
724+
offsets = super().get_offsets()
700725
if len(offsets) > 0:
701726
xs, ys = offsets.T
702727
else:
703728
xs = []
704729
ys = []
705-
self._offsets3d = juggle_axes(xs, ys, np.atleast_1d(zs), zdir)
730+
self.set_offsets3d(np.ma.column_stack((xs, ys, np.atleast_1d(zs))), zdir)
706731
# In the base draw methods we access the attributes directly which
707732
# means we cannot resolve the shuffling in the getter methods like
708733
# we do for the edge and face colors.
@@ -715,7 +740,6 @@ def set_3d_properties(self, zs, zdir):
715740
# Grab the current sizes and linewidths to preserve them.
716741
self._sizes3d = self._sizes
717742
self._linewidths3d = np.array(self._linewidths)
718-
xs, ys, zs = self._offsets3d
719743

720744
# Sort the points based on z coordinates
721745
# Performance optimization: Create a sorted index array and reorder
@@ -751,7 +775,7 @@ def set_depthshade(self, depthshade):
751775
self.stale = True
752776

753777
def do_3d_projection(self):
754-
xs, ys, zs = self._offsets3d
778+
xs, ys, zs = self.get_offsets3d()
755779
vxs, vys, vzs, vis = proj3d.proj_transform_clip(xs, ys, zs,
756780
self.axes.M)
757781
# Sort the points based on z coordinates
@@ -818,6 +842,30 @@ def get_edgecolor(self):
818842
return self.get_facecolor()
819843
return self._maybe_depth_shade_and_sort_colors(super().get_edgecolor())
820844

845+
def set_offsets3d(self, offsets, zdir='z'):
846+
"""
847+
Set the 3d offsets for the collection.
848+
849+
Parameters
850+
----------
851+
offsets : (N, 3) or (3,) array-like
852+
The offsets to be set.
853+
zdir : {'x', 'y', 'z'}
854+
The axis in which to place the offsets. Default: 'z'.
855+
See `.get_dir_vector` for a description of the values.
856+
"""
857+
return _set_offsets3d(self, offsets, zdir)
858+
859+
def get_offsets3d(self):
860+
"""Return the 3d offsets for the collection.
861+
862+
Returns
863+
-------
864+
offsets : (N, 3) array
865+
The offsets for the collection.
866+
"""
867+
return _get_offsets3d(self)
868+
821869

822870
def patch_collection_2d_to_3d(col, zs=0, zdir='z', depthshade=True):
823871
"""
@@ -1113,6 +1161,30 @@ def get_edgecolor(self):
11131161
self.do_3d_projection()
11141162
return np.asarray(self._edgecolors2d)
11151163

1164+
def set_offsets3d(self, offsets, zdir='z'):
1165+
"""
1166+
Set the 3d offsets for the collection.
1167+
1168+
Parameters
1169+
----------
1170+
offsets : (N, 3) or (3,) array-like
1171+
The offsets to be set.
1172+
zdir : {'x', 'y', 'z'}
1173+
The axis in which to place the offsets. Default: 'z'.
1174+
See `.get_dir_vector` for a description of the values.
1175+
"""
1176+
return _set_offsets3d(self, offsets, zdir)
1177+
1178+
def get_offsets3d(self):
1179+
"""Return the 3d offsets for the collection.
1180+
1181+
Returns
1182+
-------
1183+
offsets : (N, 3) array
1184+
The offsets for the collection.
1185+
"""
1186+
return _get_offsets3d(self)
1187+
11161188

11171189
def poly_collection_2d_to_3d(col, zs=0, zdir='z'):
11181190
"""
@@ -1167,6 +1239,34 @@ def rotate_axes(xs, ys, zs, zdir):
11671239
return xs, ys, zs
11681240

11691241

1242+
def _set_offsets3d(col_3d, offsets, zdir='z'):
1243+
"""
1244+
Set the 3d offsets for the collection.
1245+
1246+
Parameters
1247+
----------
1248+
offsets : (N, 3) or (3,) array-like
1249+
The offsets to be set.
1250+
zdir : {'x', 'y', 'z'}
1251+
The axis in which to place the offsets. Default: 'z'.
1252+
See `.get_dir_vector` for a description of the values.
1253+
"""
1254+
offsets = np.asanyarray(offsets)
1255+
if offsets.shape == (3,): # Broadcast (3,) -> (1, 3) but nothing else.
1256+
offsets = offsets[None, :]
1257+
xs = np.asanyarray(col_3d.convert_xunits(offsets[:, 0]), float)
1258+
ys = np.asanyarray(col_3d.convert_yunits(offsets[:, 1]), float)
1259+
zs = np.asanyarray(col_3d.convert_yunits(offsets[:, 2]), float)
1260+
col_3d._offsets3d = juggle_axes(xs, ys, np.atleast_1d(zs), zdir)
1261+
col_3d.stale = True
1262+
1263+
1264+
def _get_offsets3d(col3d):
1265+
"""Return the offsets for the collection."""
1266+
# Default to zeros in the no-offset (None) case
1267+
return np.zeros((1, 3)) if col3d._offsets3d is None else col3d._offsets3d
1268+
1269+
11701270
def _zalpha(colors, zs):
11711271
"""Modify the alphas of the color list according to depth."""
11721272
# FIXME: This only works well if the points for *zs* are well-spaced

0 commit comments

Comments
 (0)