0

I would like to use background gradients with the following DataFrame:

df = pd.DataFrame([{'Branch':'A', 'Date':datetime.datetime(2017, 1, 1), 'ratio':.3},
          {'Branch':'B', 'Date':datetime.datetime(2017, 2, 1), 'ratio':.10},
          {'Branch':'A', 'Date':datetime.datetime(2017, 2, 1), 'ratio':.40},
          {'Branch':'B', 'Date':datetime.datetime(2017, 2, 1), 'ratio':.20},
          {'Branch':'A', 'Date':datetime.datetime(2017, 3, 1), 'ratio':.80},
          {'Branch':'B', 'Date':datetime.datetime(2017, 3, 1), 'ratio':.5}
          ])
df.set_index('Date', inplace=True)
tmp = df.groupby([pd.TimeGrouper('M'),'Branch']).sum()
tmp1 = tmp.unstack('Date').sort_index(axis=1, ascending=False)

Now I would like to colorize the various fields of the DataFrame using seaborn color paletts based on a fixed scale with minimum=0 and maximum=1.

I already tried to do it via:

import seaborn as sns
cm = sns.light_palette("green", as_cmap=True)
tmp1.style.background_gradient(cmap=cm, axis=1)

But unfortunately, this uses a dynamic range of the maximum and minimum values in the various rows and not the fixed scale of minimum=0 and maximum=1

ayhan
  • 70,170
  • 20
  • 182
  • 203
Andy
  • 9,483
  • 12
  • 38
  • 39
  • Did you try the `high` and `low` parameters in tmp1.style.background_gradient(cmap=cm, axis=1, high=1, low=0)? – Scott Boston Jun 25 '17 at 19:28
  • Yes, I did try but it does not seem to work, because in that case 0.8 and 0.5 have the same color. As far as I understand http://pandas.pydata.org/pandas-docs/version/0.17.1/generated/pandas.core.style.Styler.background_gradient.html, high and low have a different meaning in this context – Andy Jun 25 '17 at 19:33
  • Take look at this [SO post](https://stackoverflow.com/questions/38931566/pandas-style-background-gradient-both-rows-and-colums?answertab=votes#tab-top) – Scott Boston Jun 25 '17 at 19:40
  • 1
    Thanks, I have already seen the post, however, I was hoping that there is an easier way to do it. – Andy Jun 25 '17 at 19:52

0 Answers0