Pandas: background_gradient vmin and vmax

Created on 26 Jan 2016  路  6Comments  路  Source: pandas-dev/pandas

Working with the new style API, I would like to turn several columns of a dataframe into something like a heatmap. I would like to use the same range for all columns in the subset, i.e., the colour map should take a minimum and maximum value I specify, regardless of what actual range each column has. Currently using the "min" and "max" this is not intuitive. I would much prefer to be able to specify explicit vmin and vmax values, analogous to the seaborn.heatmap function.

Thanks for the style API, extremely useful.

IO HTML good first issue

Most helpful comment

FYI: axis = None support is being added under PR #21259

All 6 comments

That's what I wanted initially as well, but didn't have time to do it properly before the last release. I wonder if we should just require seaborn for those parameters, and use its heuristics for determining a good colormap and handling vmin/vmax.

Hi,

I have some feedback which IIUC is related to what @alimanfoo is saying. The background_gradient as well as the bar style methods should have the same axis options as the highlight_max method:

axis: int, str, or None; default None

It can be very misleading if the color scale is only applied to each column separately - particulary when I specify a subset of columns (which represent the same measure) I expected them to be represented with the same color scale by default. I tried to set axis=None for the above mentioned methods, but unluckily an error is being raised from that.
I get that the feature is new and hopefully this will be possible soon. Apart from my negative criticism: Kudo's for the good work.

Couldn't agree more with @mpschr --- axis = None should even be the default. Anybody know a workaround for now?

@alimanfoo I'm not sure it applies directly to your problem, but to get the equivalent of vmin/vmax for the colormap, I do the following:

# given df, cmap, vmin, vmax:
df_data = df.copy()
df = df.clip(vmin, vmax)
df = df.style.background_gradient(cmap=cmap)
df.data = df_data

FYI: axis = None support is being added under PR #21259

Thanks everyone, much appreciated.

Was this page helpful?
0 / 5 - 0 ratings