-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
ENH: Styler.background_gradient to accept vmin vmax and dtype Int64 #29245
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
9e89e6c
1231618
905d3b9
cecf5bd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -958,6 +958,8 @@ def background_gradient( | |
axis=0, | ||
subset=None, | ||
text_color_threshold=0.408, | ||
vmin=None, | ||
vmax=None, | ||
): | ||
""" | ||
Color the background in a gradient according to | ||
|
@@ -986,6 +988,18 @@ def background_gradient( | |
|
||
.. versionadded:: 0.24.0 | ||
|
||
vmin : float, optional | ||
Minimum data value that corresponds to colormap minimum value. | ||
When None (default): the minimum value of the data will be used. | ||
|
||
.. versionadded:: 1.0.0 | ||
|
||
vmax : float, optional | ||
Maximum data value that corresponds to colormap maximum value. | ||
When None (default): the maximum value of the data will be used. | ||
|
||
.. versionadded:: 1.0.0 | ||
|
||
Returns | ||
------- | ||
self : Styler | ||
|
@@ -1012,11 +1026,15 @@ def background_gradient( | |
low=low, | ||
high=high, | ||
text_color_threshold=text_color_threshold, | ||
vmin=vmin, | ||
vmax=vmax, | ||
) | ||
return self | ||
|
||
@staticmethod | ||
def _background_gradient(s, cmap="PuBu", low=0, high=0, text_color_threshold=0.408): | ||
def _background_gradient( | ||
s, cmap="PuBu", low=0, high=0, text_color_threshold=0.408, vmin=None, vmax=None | ||
): | ||
""" | ||
Color background in a range according to the data. | ||
""" | ||
|
@@ -1028,14 +1046,18 @@ def _background_gradient(s, cmap="PuBu", low=0, high=0, text_color_threshold=0.4 | |
raise ValueError(msg) | ||
|
||
with _mpl(Styler.background_gradient) as (plt, colors): | ||
smin = s.values.min() | ||
smax = s.values.max() | ||
smin = s.min() if vmin is None else vmin | ||
if isinstance(smin, ABCSeries): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this not already covered by the line directly preceding it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here depends on the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. side note: try to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you move this into a helper function? I think this affects
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! didn't notice the |
||
smin = smin.min() | ||
smax = s.max() if vmax is None else vmax | ||
if isinstance(smax, ABCSeries): | ||
smax = smax.max() | ||
rng = smax - smin | ||
# extend lower / upper bounds, compresses color range | ||
norm = colors.Normalize(smin - (rng * low), smax + (rng * high)) | ||
# matplotlib colors.Normalize modifies inplace? | ||
# https://github.com/matplotlib/matplotlib/issues/5427 | ||
rgbas = plt.cm.get_cmap(cmap)(norm(s.values)) | ||
rgbas = plt.cm.get_cmap(cmap)(norm(s.to_numpy(dtype=float))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK with this but can just use default constructor without There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unfortunately, |
||
|
||
def relative_luminance(rgba): | ||
""" | ||
|
@@ -1121,7 +1143,7 @@ def _bar(s, align, colors, width=100, vmin=None, vmax=None): | |
smax = max(abs(smin), abs(smax)) | ||
smin = -smax | ||
# Transform to percent-range of linear-gradient | ||
normed = width * (s.values - smin) / (smax - smin + 1e-12) | ||
normed = width * (s.to_numpy(dtype=float) - smin) / (smax - smin + 1e-12) | ||
zero = -width * smin / (smax - smin + 1e-12) | ||
|
||
def css_bar(start, end, color): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you annotate the new parameters?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your review @WillAyd, but I don't quite get it what do you mean by "annotate", can you elaborate a little more? (I have docstring already so I suppose you don't mean docstring right?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I got it! you mean the type-hints annotation right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes that is correct
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done! 😄