@@ -958,6 +958,8 @@ def background_gradient(
958
958
axis = 0 ,
959
959
subset = None ,
960
960
text_color_threshold = 0.408 ,
961
+ vmin = None ,
962
+ vmax = None ,
961
963
):
962
964
"""
963
965
Color the background in a gradient according to
@@ -986,6 +988,18 @@ def background_gradient(
986
988
987
989
.. versionadded:: 0.24.0
988
990
991
+ vmin : float, optional
992
+ Minimum data value that corresponds to colormap minimum value.
993
+ When None (default): the minimum value of the data will be used.
994
+
995
+ .. versionadded:: 1.0.0
996
+
997
+ vmax : float, optional
998
+ Maximum data value that corresponds to colormap maximum value.
999
+ When None (default): the maximum value of the data will be used.
1000
+
1001
+ .. versionadded:: 1.0.0
1002
+
989
1003
Returns
990
1004
-------
991
1005
self : Styler
@@ -1012,11 +1026,15 @@ def background_gradient(
1012
1026
low = low ,
1013
1027
high = high ,
1014
1028
text_color_threshold = text_color_threshold ,
1029
+ vmin = vmin ,
1030
+ vmax = vmax ,
1015
1031
)
1016
1032
return self
1017
1033
1018
1034
@staticmethod
1019
- def _background_gradient (s , cmap = "PuBu" , low = 0 , high = 0 , text_color_threshold = 0.408 ):
1035
+ def _background_gradient (
1036
+ s , cmap = "PuBu" , low = 0 , high = 0 , text_color_threshold = 0.408 , vmin = None , vmax = None
1037
+ ):
1020
1038
"""
1021
1039
Color background in a range according to the data.
1022
1040
"""
@@ -1028,14 +1046,18 @@ def _background_gradient(s, cmap="PuBu", low=0, high=0, text_color_threshold=0.4
1028
1046
raise ValueError (msg )
1029
1047
1030
1048
with _mpl (Styler .background_gradient ) as (plt , colors ):
1031
- smin = s .values .min ()
1032
- smax = s .values .max ()
1049
+ smin = s .min () if vmin is None else vmin
1050
+ if isinstance (smin , ABCSeries ):
1051
+ smin = smin .min ()
1052
+ smax = s .max () if vmax is None else vmax
1053
+ if isinstance (smax , ABCSeries ):
1054
+ smax = smax .max ()
1033
1055
rng = smax - smin
1034
1056
# extend lower / upper bounds, compresses color range
1035
1057
norm = colors .Normalize (smin - (rng * low ), smax + (rng * high ))
1036
1058
# matplotlib colors.Normalize modifies inplace?
1037
1059
# https://github.com/matplotlib/matplotlib/issues/5427
1038
- rgbas = plt .cm .get_cmap (cmap )(norm (s .values ))
1060
+ rgbas = plt .cm .get_cmap (cmap )(norm (s .to_numpy ( dtype = float ) ))
1039
1061
1040
1062
def relative_luminance (rgba ):
1041
1063
"""
@@ -1121,7 +1143,7 @@ def _bar(s, align, colors, width=100, vmin=None, vmax=None):
1121
1143
smax = max (abs (smin ), abs (smax ))
1122
1144
smin = - smax
1123
1145
# Transform to percent-range of linear-gradient
1124
- normed = width * (s .values - smin ) / (smax - smin + 1e-12 )
1146
+ normed = width * (s .to_numpy ( dtype = float ) - smin ) / (smax - smin + 1e-12 )
1125
1147
zero = - width * smin / (smax - smin + 1e-12 )
1126
1148
1127
1149
def css_bar (start , end , color ):
0 commit comments