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

Skip to content

Commit 0a41236

Browse files
Remove legacy and inverted depth shade options
1 parent 3f67541 commit 0a41236

File tree

3 files changed

+15
-131
lines changed

3 files changed

+15
-131
lines changed
Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,17 @@
1-
Depth-shading fix and more depth-shading options
2-
--------------------------------------------------------------
3-
4-
New options have been added which allow users to modify the behavior of
5-
depth-shading while addressing a visual bug.
1+
3D depth-shading fix
2+
--------------------
63

74
Previously, a slightly buggy method of estimating the "depth" of plotted
85
items could lead to sudden and unexpected changes in transparency as the
96
plot orientation changed.
107

11-
Now, the behavior has been made smooth and predictable, and the user is
12-
provided with three new options: whether to invert the shading, setting the
13-
lowest acceptable alpha value (highest transparency), and whether to use
14-
the old algorithm.
15-
16-
The default behavior visually matches the old algorithm: items that appear to be
17-
"deeper" into the screen will become increasingly transparent (up to the now
18-
user-defined limit). If the inversion option is used then items will start
19-
at maximum transparency and become gradually opaque with increasing depth.
20-
21-
Note 1: depth-shading applies to Patch3DCollections and Path3DCollections,
22-
including scatter plots.
8+
Now, the behavior has been made smooth and predictable. A new parameter
9+
``depthshade_minalpha`` has also been added to allow users to set the minimum
10+
transparency level.
2311

24-
Note 2: "depthshade=True" must still be used to enable depth-shading
12+
Depth-shading is an option for Patch3DCollections and Path3DCollections,
13+
including 3D scatter plots. Depth-shading is still off by default, and
14+
``depthshade=True`` must still be used to enable it.
2515

2616
A simple example:
2717

@@ -42,9 +32,7 @@ A simple example:
4232
zs=Z,
4333
s=S,
4434
depthshade=True,
45-
depthshade_minalpha=0.1,
46-
depthshade_inverted=True,
47-
depthshade_legacy=True,
35+
depthshade_minalpha=0.3,
4836
)
4937

5038
plt.show()

lib/mpl_toolkits/mplot3d/art3d.py

Lines changed: 6 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -633,9 +633,7 @@ def __init__(
633633
zs=0,
634634
zdir="z",
635635
depthshade=True,
636-
depthshade_inverted=False,
637636
depthshade_minalpha=0.3,
638-
depthshade_legacy=False,
639637
axlim_clip=False,
640638
**kwargs
641639
):
@@ -654,40 +652,24 @@ def __init__(
654652
give the appearance of depth (default is *True*).
655653
This is typically desired in scatter plots.
656654
657-
*depthshade_inverted* sets whether to reverse the order of
658-
depth-shading transparency.
659-
660655
*depthshade_minalpha* sets the minimum alpha value applied by
661656
depth-shading.
662-
663-
*depthshade_legacy* sets whether to use the legacy algorithm
664-
for depth-shading.
665657
"""
666658
self._depthshade = depthshade
667-
self._depthshade_inverted = depthshade_inverted
668659
self._depthshade_minalpha = depthshade_minalpha
669-
self._depthshade_legacy = depthshade_legacy
670660
super().__init__(*args, **kwargs)
671661
self.set_3d_properties(zs, zdir, axlim_clip)
672662

673663
def get_depthshade(self):
674664
return self._depthshade
675665

676-
def get_depthshade_inverted(self):
677-
return self._depthshade_inverted
678-
679666
def get_depthshade_minalpha(self):
680667
return self._depthshade_minalpha
681668

682-
def get_depthshade_legacy(self):
683-
return self._depthshade_legacy
684-
685669
def set_depthshade(
686670
self,
687671
depthshade,
688-
depthshade_inverted=False,
689672
depthshade_minalpha=0.3,
690-
depthshade_legacy=False,
691673
):
692674
"""
693675
Set whether depth shading is performed on collection members.
@@ -697,17 +679,11 @@ def set_depthshade(
697679
depthshade : bool
698680
Whether to shade the patches in order to give the appearance of
699681
depth.
700-
depthshade_inverted : bool
701-
Whether to reverse order of depth-shading transparency.
702682
depthshade_minalpha : float
703683
Sets the minimum alpha value used by depth-shading.
704-
depthshade_legacy : bool
705-
Whtether to use the legacy algorithm for depth-shading.
706684
"""
707685
self._depthshade = depthshade
708-
self._depthshade_inverted = depthshade_inverted
709686
self._depthshade_minalpha = depthshade_minalpha
710-
self._depthshade_legacy = depthshade_legacy
711687
self.stale = True
712688

713689
def set_sort_zpos(self, val):
@@ -767,9 +743,7 @@ def _maybe_depth_shade_and_sort_colors(self, color_array):
767743
_zalpha(
768744
color_array,
769745
self._vzs,
770-
inverted=self._depthshade_inverted,
771746
min_alpha=self._depthshade_minalpha,
772-
legacy=self._depthshade_legacy,
773747
)
774748
if self._vzs is not None and self._depthshade
775749
else color_array
@@ -813,9 +787,7 @@ def __init__(
813787
zs=0,
814788
zdir="z",
815789
depthshade=True,
816-
depthshade_inverted=False,
817790
depthshade_minalpha=0.3,
818-
depthshade_legacy=False,
819791
axlim_clip=False,
820792
**kwargs
821793
):
@@ -834,19 +806,11 @@ def __init__(
834806
give the appearance of depth (default is *True*).
835807
This is typically desired in scatter plots.
836808
837-
*depthshade_inverted* sets whether to reverse the order of
838-
depth-shading transparency.
839-
840809
*depthshade_minalpha* sets the minimum alpha value applied by
841810
depth-shading.
842-
843-
*depthshade_legacy* sets whether to use the legacy algorithm
844-
for depth-shading.
845811
"""
846812
self._depthshade = depthshade
847-
self._depthshade_inverted = depthshade_inverted
848813
self._depthshade_minalpha = depthshade_minalpha
849-
self._depthshade_legacy = depthshade_legacy
850814
self._in_draw = False
851815
super().__init__(*args, **kwargs)
852816
self.set_3d_properties(zs, zdir, axlim_clip)
@@ -925,21 +889,13 @@ def set_linewidth(self, lw):
925889
def get_depthshade(self):
926890
return self._depthshade
927891

928-
def get_depthshade_inverted(self):
929-
return self._depthshade_inverted
930-
931892
def get_depthshade_minalpha(self):
932893
return self._depthshade_minalpha
933894

934-
def get_depthshade_legacy(self):
935-
return self._depthshade_legacy
936-
937895
def set_depthshade(
938896
self,
939897
depthshade,
940-
depthshade_inverted=False,
941898
depthshade_minalpha=0.3,
942-
depthshade_legacy=False,
943899
):
944900
"""
945901
Set whether depth shading is performed on collection members.
@@ -949,17 +905,11 @@ def set_depthshade(
949905
depthshade : bool
950906
Whether to shade the patches in order to give the appearance of
951907
depth.
952-
depthshade_inverted : bool
953-
Whether to reverse order of depth-shading transparency.
954908
depthshade_minalpha : float
955909
Sets the minimum alpha value used by depth-shading.
956-
depthshade_legacy : bool
957-
Whtether to use the legacy algorithm for depth-shading.
958910
"""
959911
self._depthshade = depthshade
960-
self._depthshade_inverted = depthshade_inverted
961912
self._depthshade_minalpha = depthshade_minalpha
962-
self._depthshade_legacy = depthshade_legacy
963913
self.stale = True
964914

965915
def do_3d_projection(self):
@@ -1021,9 +971,7 @@ def _maybe_depth_shade_and_sort_colors(self, color_array):
1021971
color_array = _zalpha(
1022972
color_array,
1023973
self._vzs,
1024-
inverted=self._depthshade_inverted,
1025974
min_alpha=self._depthshade_minalpha,
1026-
legacy=self._depthshade_legacy,
1027975
_data_scale=self._data_scale,
1028976
)
1029977

@@ -1051,9 +999,7 @@ def patch_collection_2d_to_3d(
1051999
zs=0,
10521000
zdir="z",
10531001
depthshade=True,
1054-
depthshade_inverted=False,
10551002
depthshade_minalpha=0.3,
1056-
depthshade_legacy=False,
10571003
axlim_clip=False,
10581004
):
10591005
"""
@@ -1073,12 +1019,8 @@ def patch_collection_2d_to_3d(
10731019
See `.get_dir_vector` for a description of the values.
10741020
depthshade
10751021
Whether to shade the patches to give a sense of depth. Default: *True*.
1076-
depthshade_invert
1077-
Whether to reverse order of depth-shading transparency. Default: *False*.
10781022
depthshade_minalpha
10791023
Sets the minimum alpha value used by depth-shading. Default: 0.3.
1080-
depthshade_legacy
1081-
Whether to use the legacy algorithm for depth-shading. Default: *False*.
10821024
axlim_clip : bool, default: False
10831025
Whether to hide patches with a vertex outside the axes view limits.
10841026
@@ -1089,9 +1031,7 @@ def patch_collection_2d_to_3d(
10891031
elif isinstance(col, PatchCollection):
10901032
col.__class__ = Patch3DCollection
10911033
col._depthshade = depthshade
1092-
col._depthshade_inverted = depthshade_inverted
10931034
col._depthshade_minalpha = depthshade_minalpha
1094-
col._depthshade_legacy = depthshade_legacy
10951035
col._in_draw = False
10961036
col.set_3d_properties(zs, zdir, axlim_clip)
10971037

@@ -1435,9 +1375,7 @@ def rotate_axes(xs, ys, zs, zdir):
14351375
def _zalpha(
14361376
colors,
14371377
zs,
1438-
inverted=False,
14391378
min_alpha=0.3,
1440-
legacy=False,
14411379
_data_scale=None,
14421380
):
14431381
"""Modify the alphas of the color list according to depth."""
@@ -1448,49 +1386,17 @@ def _zalpha(
14481386
# Alpha values beyond the range 0-1 inclusive make no sense, so clip them
14491387
min_alpha = np.clip(min_alpha, 0, 1)
14501388

1451-
if _data_scale is None or legacy:
1452-
# Revert to "legacy mode" if the new method of calculating
1453-
# _data_scale fails, or if the user asks for it
1454-
1455-
# This only works well if the points for *zs* are well-spaced in
1456-
# all three dimensions. Otherwise, at certain orientations the
1457-
# min and max zs are very close together.
1458-
# Should really normalize against the viewing depth.
1459-
1460-
# Normalize the z-depths to the range 0 - 1
1461-
norm = Normalize(min(zs), max(zs))
1462-
1463-
# Generate alpha multipliers using the normalized z-depths so that
1464-
# closer points are opaque and the furthest points are still visible,
1465-
# but transparent
1466-
if inverted:
1467-
sats = norm(zs) * (1 - min_alpha) + min_alpha
1468-
else:
1469-
sats = 1 - norm(zs) * (1 - min_alpha)
1389+
if _data_scale is None or _data_scale == 0:
1390+
# Don't scale the alpha values since we have no valid data scale for reference
1391+
sats = np.ones_like(zs)
14701392

14711393
else:
1472-
# Improved normalization using a scale value derived from the XYZ
1473-
# limits of the plot
1474-
1475-
if _data_scale == 0:
1476-
# Don't scale the alpha values since we have no valid
1477-
# data scale for reference
1478-
sats = np.ones_like(zs)
1479-
1480-
else:
1481-
if inverted:
1482-
# Deeper points have an increasingly solid appearance
1483-
sats = np.clip(1 - (max(zs) - zs) / _data_scale, min_alpha, 1)
1484-
else:
1485-
# This is the mode that most closely matches the legacy behavior
1486-
1487-
# Deeper points have an increasingly transparent appearance
1488-
sats = np.clip(1 - (zs - min(zs)) / _data_scale, min_alpha, 1)
1394+
# Deeper points have an increasingly transparent appearance
1395+
sats = np.clip(1 - (zs - min(zs)) / _data_scale, min_alpha, 1)
14891396

14901397
rgba = np.broadcast_to(mcolors.to_rgba_array(colors), (len(zs), 4))
14911398

1492-
# Change the alpha values of the colors using the generated alpha
1493-
# multipliers
1399+
# Change the alpha values of the colors using the generated alpha multipliers
14941400
return np.column_stack([rgba[:, :3], rgba[:, 3] * sats])
14951401

14961402

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2904,9 +2904,7 @@ def add_collection3d(self, col, zs=0, zdir='z', autolim=True, *,
29042904
"edgecolors", "c", "facecolor",
29052905
"facecolors", "color"])
29062906
def scatter(self, xs, ys, zs=0, zdir='z', s=20, c=None, depthshade=True,
2907-
depthshade_inverted=False,
29082907
depthshade_minalpha=0.3,
2909-
depthshade_legacy=False,
29102908
*args,
29112909
axlim_clip=False,
29122910
**kwargs):
@@ -2946,15 +2944,9 @@ def scatter(self, xs, ys, zs=0, zdir='z', s=20, c=None, depthshade=True,
29462944
depth. Each call to ``scatter()`` will perform its depthshading
29472945
independently.
29482946
2949-
depthshade_inverted : bool, default: False
2950-
Whether to reverse the order of depth-shading transparency.
2951-
29522947
depthshade_minalpha : float, default: 0.3
29532948
The lowest alpha value applied by depth-shading.
29542949
2955-
depthshade_legacy : bool, default: False
2956-
Whether to use the legacy algorithm for depth-shading.
2957-
29582950
axlim_clip : bool, default: False
29592951
Whether to hide the scatter points outside the axes view limits.
29602952
@@ -2993,9 +2985,7 @@ def scatter(self, xs, ys, zs=0, zdir='z', s=20, c=None, depthshade=True,
29932985
zs=zs,
29942986
zdir=zdir,
29952987
depthshade=depthshade,
2996-
depthshade_inverted=depthshade_inverted,
29972988
depthshade_minalpha=depthshade_minalpha,
2998-
depthshade_legacy=depthshade_legacy,
29992989
axlim_clip=axlim_clip,
30002990
)
30012991

0 commit comments

Comments
 (0)