Skip to content

Commit e353a99

Browse files
Make axes3d.depthshade and axes3d.depthshade_minalpha rcparams
1 parent 9e3e00d commit e353a99

File tree

6 files changed

+58
-26
lines changed

6 files changed

+58
-26
lines changed

doc/users/next_whats_new/depthshading_improvement.rst

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
3D depth-shading fix
22
--------------------
33

4-
Previously, a slightly buggy method of estimating the "depth" of plotted
5-
items could lead to sudden and unexpected changes in transparency as the
6-
plot orientation changed.
4+
Previously, a slightly buggy method of estimating the visual "depth" of 3D
5+
items could lead to sudden and unexpected changes in transparency as the plot
6+
orientation changed.
77

88
Now, the behavior has been made smooth and predictable. A new parameter
99
``depthshade_minalpha`` has also been added to allow users to set the minimum
10-
transparency level.
10+
transparency level. Depth-shading is an option for Patch3DCollections and
11+
Path3DCollections, including 3D scatter plots.
1112

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.
13+
The default values for ``depthshade`` and ``depthshade_minalpha`` are now also
14+
controlled via rcParams, with values of ``True`` and ``0.3`` respectively.
1515

1616
A simple example:
1717

lib/matplotlib/mpl-data/matplotlibrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,9 @@
439439
#axes3d.yaxis.panecolor: (0.90, 0.90, 0.90, 0.5) # background pane on 3D axes
440440
#axes3d.zaxis.panecolor: (0.925, 0.925, 0.925, 0.5) # background pane on 3D axes
441441

442+
#axes3d.depthshade: True # depth shade for 3D scatter plots
443+
#axes3d.depthshade_minalpha: 0.3 # minimum alpha value for depth shading
444+
442445
#axes3d.mouserotationstyle: arcball # {azel, trackball, sphere, arcball}
443446
# See also https://matplotlib.org/stable/api/toolkits/mplot3d/view_angles.html#rotation-with-mouse
444447
#axes3d.trackballsize: 0.667 # trackball diameter, in units of the Axes bbox

lib/matplotlib/mpl-data/stylelib/classic.mplstyle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,8 @@ axes.spines.top : True
225225
polaraxes.grid : True # display grid on polar axes
226226
axes3d.grid : True # display grid on 3D axes
227227
axes3d.automargin : False # automatically add margin when manually setting 3D axis limits
228+
axes3d.depthshade : False # depth shade for 3D scatter plots
229+
axes3d.depthshade_minalpha : 0.3 # minimum alpha value for depth shading
228230

229231
date.autoformatter.year : %Y
230232
date.autoformatter.month : %b %Y

lib/matplotlib/rcsetup.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,6 +1125,9 @@ def _convert_validator_spec(key, conv):
11251125
"axes3d.yaxis.panecolor": validate_color, # 3d background pane
11261126
"axes3d.zaxis.panecolor": validate_color, # 3d background pane
11271127

1128+
"axes3d.depthshade": validate_bool, # depth shade for 3D scatter plots
1129+
"axes3d.depthshade_minalpha": validate_float, # min alpha value for depth shading
1130+
11281131
"axes3d.mouserotationstyle": ["azel", "trackball", "sphere", "arcball"],
11291132
"axes3d.trackballsize": validate_float,
11301133
"axes3d.trackballborder": validate_float,

lib/mpl_toolkits/mplot3d/art3d.py

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
from matplotlib import (
1717
_api, artist, cbook, colors as mcolors, lines, text as mtext,
18-
path as mpath)
18+
path as mpath, rcParams)
1919
from matplotlib.collections import (
2020
Collection, LineCollection, PolyCollection, PatchCollection, PathCollection)
2121
from matplotlib.patches import Patch
@@ -631,8 +631,8 @@ def __init__(
631631
*args,
632632
zs=0,
633633
zdir="z",
634-
depthshade=True,
635-
depthshade_minalpha=0.3,
634+
depthshade=None,
635+
depthshade_minalpha=None,
636636
axlim_clip=False,
637637
**kwargs
638638
):
@@ -654,6 +654,10 @@ def __init__(
654654
*depthshade_minalpha* sets the minimum alpha value applied by
655655
depth-shading.
656656
"""
657+
if depthshade is None:
658+
depthshade = rcParams['axes3d.depthshade']
659+
if depthshade_minalpha is None:
660+
depthshade_minalpha = rcParams['axes3d.depthshade_minalpha']
657661
self._depthshade = depthshade
658662
self._depthshade_minalpha = depthshade_minalpha
659663
super().__init__(*args, **kwargs)
@@ -665,7 +669,7 @@ def get_depthshade(self):
665669
def set_depthshade(
666670
self,
667671
depthshade,
668-
depthshade_minalpha=0.3,
672+
depthshade_minalpha=None,
669673
):
670674
"""
671675
Set whether depth shading is performed on collection members.
@@ -675,9 +679,12 @@ def set_depthshade(
675679
depthshade : bool
676680
Whether to shade the patches in order to give the appearance of
677681
depth.
678-
depthshade_minalpha : float
682+
depthshade_minalpha : float, default: None
679683
Sets the minimum alpha value used by depth-shading.
684+
If None, use the value from rcParams['axes3d.depthshade_minalpha'].
680685
"""
686+
if depthshade_minalpha is None:
687+
depthshade_minalpha = rcParams['axes3d.depthshade_minalpha']
681688
self._depthshade = depthshade
682689
self._depthshade_minalpha = depthshade_minalpha
683690
self.stale = True
@@ -793,8 +800,8 @@ def __init__(
793800
*args,
794801
zs=0,
795802
zdir="z",
796-
depthshade=True,
797-
depthshade_minalpha=0.3,
803+
depthshade=None,
804+
depthshade_minalpha=None,
798805
axlim_clip=False,
799806
**kwargs
800807
):
@@ -816,6 +823,10 @@ def __init__(
816823
*depthshade_minalpha* sets the minimum alpha value applied by
817824
depth-shading.
818825
"""
826+
if depthshade is None:
827+
depthshade = rcParams['axes3d.depthshade']
828+
if depthshade_minalpha is None:
829+
depthshade_minalpha = rcParams['axes3d.depthshade_minalpha']
819830
self._depthshade = depthshade
820831
self._depthshade_minalpha = depthshade_minalpha
821832
self._in_draw = False
@@ -899,7 +910,7 @@ def get_depthshade(self):
899910
def set_depthshade(
900911
self,
901912
depthshade,
902-
depthshade_minalpha=0.3,
913+
depthshade_minalpha=None,
903914
):
904915
"""
905916
Set whether depth shading is performed on collection members.
@@ -912,6 +923,8 @@ def set_depthshade(
912923
depthshade_minalpha : float
913924
Sets the minimum alpha value used by depth-shading.
914925
"""
926+
if depthshade_minalpha is None:
927+
depthshade_minalpha = rcParams['axes3d.depthshade_minalpha']
915928
self._depthshade = depthshade
916929
self._depthshade_minalpha = depthshade_minalpha
917930
self.stale = True
@@ -1002,10 +1015,10 @@ def patch_collection_2d_to_3d(
10021015
col,
10031016
zs=0,
10041017
zdir="z",
1005-
depthshade=True,
1018+
depthshade=None,
10061019
axlim_clip=False,
10071020
*args,
1008-
depthshade_minalpha=0.3
1021+
depthshade_minalpha=None,
10091022
):
10101023
"""
10111024
Convert a `.PatchCollection` into a `.Patch3DCollection` object
@@ -1022,10 +1035,12 @@ def patch_collection_2d_to_3d(
10221035
zdir : {'x', 'y', 'z'}
10231036
The axis in which to place the patches. Default: "z".
10241037
See `.get_dir_vector` for a description of the values.
1025-
depthshade
1026-
Whether to shade the patches to give a sense of depth. Default: *True*.
1027-
depthshade_minalpha
1028-
Sets the minimum alpha value used by depth-shading. Default: 0.3.
1038+
depthshade : bool, default: None
1039+
Whether to shade the patches to give a sense of depth.
1040+
If None, use the value from rcParams['axes3d.depthshade'].
1041+
depthshade_minalpha : float, default: None
1042+
Sets the minimum alpha value used by depth-shading.
1043+
If None, use the value from rcParams['axes3d.depthshade_minalpha'].
10291044
axlim_clip : bool, default: False
10301045
Whether to hide patches with a vertex outside the axes view limits.
10311046
"""
@@ -1034,6 +1049,10 @@ def patch_collection_2d_to_3d(
10341049
col._offset_zordered = None
10351050
elif isinstance(col, PatchCollection):
10361051
col.__class__ = Patch3DCollection
1052+
if depthshade is None:
1053+
depthshade = rcParams['axes3d.depthshade']
1054+
if depthshade_minalpha is None:
1055+
depthshade_minalpha = rcParams['axes3d.depthshade_minalpha']
10371056
col._depthshade = depthshade
10381057
col._depthshade_minalpha = depthshade_minalpha
10391058
col._in_draw = False

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2904,9 +2904,9 @@ def add_collection3d(self, col, zs=0, zdir='z', autolim=True, *,
29042904
@_preprocess_data(replace_names=["xs", "ys", "zs", "s",
29052905
"edgecolors", "c", "facecolor",
29062906
"facecolors", "color"])
2907-
def scatter(self, xs, ys, zs=0, zdir='z', s=20, c=None, depthshade=True,
2907+
def scatter(self, xs, ys, zs=0, zdir='z', s=20, c=None, depthshade=None,
29082908
*args,
2909-
depthshade_minalpha=0.3,
2909+
depthshade_minalpha=None,
29102910
axlim_clip=False,
29112911
**kwargs):
29122912
"""
@@ -2940,14 +2940,15 @@ def scatter(self, xs, ys, zs=0, zdir='z', s=20, c=None, depthshade=True,
29402940
- A 2D array in which the rows are RGB or RGBA.
29412941
29422942
For more details see the *c* argument of `~.axes.Axes.scatter`.
2943-
depthshade : bool, default: True
2943+
depthshade : bool, default: None
29442944
Whether to shade the scatter markers to give the appearance of
29452945
depth. Each call to ``scatter()`` will perform its depthshading
29462946
independently.
2947+
If None, use the value from rcParams['axes3d.depthshade'].
29472948
2948-
depthshade_minalpha : float, default: 0.3
2949+
depthshade_minalpha : float, default: None
29492950
The lowest alpha value applied by depth-shading.
2950-
2951+
If None, use the value from rcParams['axes3d.depthshade_minalpha'].
29512952
axlim_clip : bool, default: False
29522953
Whether to hide the scatter points outside the axes view limits.
29532954
@@ -2975,6 +2976,10 @@ def scatter(self, xs, ys, zs=0, zdir='z', s=20, c=None, depthshade=True,
29752976
)
29762977
if kwargs.get("color") is not None:
29772978
kwargs['color'] = color
2979+
if depthshade is None:
2980+
depthshade = mpl.rcParams['axes3d.depthshade']
2981+
if depthshade_minalpha is None:
2982+
depthshade_minalpha = mpl.rcParams['axes3d.depthshade_minalpha']
29782983

29792984
# For xs and ys, 2D scatter() will do the copying.
29802985
if np.may_share_memory(zs_orig, zs): # Avoid unnecessary copies.

0 commit comments

Comments
 (0)