-1

I have a pandas dataframe that I will enclose below. I was looking for a module that would help me display this data in a way that lets me change the colour of the number based on it's value. The higher the value the more blue it would be, the lower the value the less blue it would be.

       Jan   Feb   Mar   Apr   May   Jun   Jul   Aug   Sep   Oct   Nov   Dec
year                                                                        
2020  4.34  4.34  4.34  4.34  4.34  4.34  4.34  4.34  4.34  4.34  4.34  4.34
2018  3.34  3.34  3.34  3.34  3.34  3.34  3.34  3.34  3.34  3.34  3.34  3.34
2018  5.34  5.34  5.34  5.34  5.34  5.34  5.34  5.34  5.34  5.34  5.34  5.34
2017  2.39  2.39  2.39  2.39  2.39  2.39  2.39  2.39  2.39  2.39  2.39  2.39
2016  3.93  3.93  3.93  3.93  3.93  3.93  3.93  3.93  3.93  3.93  3.93  3.93
2015  4.32  4.32  4.32  4.32  4.32  4.32  4.32  4.32  4.32  4.32  4.32  4.32

The photo below shows a colour system that I'd ideally like to make my data frame look like. If anybody knows how to do this on Matplotlib or any other module I'd appreciate it, thanks. https://gyazo.com/bea759dd38abcefc64106461c2111abd

Faiz
  • 143
  • 1
  • 10

1 Answers1

0

Use Styler.background_gradient:

df = pd.DataFrame(np.random.randint(-10, 10, (6, 12)),
                  columns=pd.Index(['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 
                                    'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']),
                  index=pd.Index([2020, 2019, 2018, 2017, 2016, 2015], name='year'))

df.style.background_gradient(axis=None).to_excel('data.xlsx')

Styler.background_gradient

Corralien
  • 109,409
  • 8
  • 28
  • 52