-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
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
ENH: Allow elementwise coloring in background_gradient with axis=None #15204
Comments
My workaround: def background_gradient(self, cvals=None, cmin=None, cmax=None, cmap='viridis', **css):
"""For use with `DataFrame.style.apply` this function will apply a heatmap
color gradient *elementwise* to the calling DataFrame
Parameters
----------
self : pd.DataFrame
The calling DataFrame. This argument is automatically passed in by the
`DataFrame.style.apply` method
cvals : pd.DataFrame
If specified this DataFrame is used to determine the color gradient
cmin : float
If specified, any values below this will be clipped to the bottom of
the cmap
cmax : float
If specified, any values above this will be clipped to the top of
the cmap
cmap : colormap or str
The colormap to use
css : dict
Any extra inline css key/value pars to pass to the styler
Returns
-------
pd.DataFrame
The css styles to apply
"""
from matplotlib import cm
from matplotlib.colors import rgb2hex
if cvals is None:
cvals = self.values.ravel().copy()
else:
assert cvals.shape == self.shape
cvals = cvals.values.ravel().copy()
cvals -= cmin or cvals.min()
cvals /= cmax or cvals.max()
cvals = cvals.clip(0, 1)
styles = []
for rgb in cm.viridis_r(cvals):
style = [
"{}: {}".format(key.replace('_', '-'), value)
for key, value in css.items()
]
style.append("background-color: {}".format(rgb2hex(rgb)))
styles.append('; '.join(style))
styles = np.asarray(styles).reshape(self.shape)
return pd.DataFrame(styles, index=self.index, columns=self.columns) |
Sounds logical, PR certainly welcome I think! |
Yep, that's be great if you could submit a PR. |
this should be fairly generalized i think, iow where we accept axis |
@dhirschfeld Interested to do a PR for this? (I also stumbled into a case where I would need this) BTW, your ability to specify a vmin/vmax is also a nice feature I think. |
@jorisvandenbossche - I'd like to finish it up by submitting a PR but it's unlikely I'll have time in at least the next couple of weeks so I'd be happy for anyone else interested to pick it up. In the interim, my implementation posted above is available for anyone who finds it... I had a need to explicitly specify the cmin/cmax values so I implemented that directly rather than the high/low compression. I also thought the |
At present the axis argument to
background_gradient
only accepts 0 or 1 so that the resulting "heatmap" can only be applied columnwise or rowwise rather than to the whole table.I propose that
axis=None
be allowed to mean applying a color gradient elementwise.The text was updated successfully, but these errors were encountered: