-1

I have the following table on python:

Image

What I want to do is see the table in a nicer looking way, with the cells going from red to blue depending on their value, a bit like this:

RdBl

Does anyone know how to do that?

I've done this so far but don't know what to put in the vars options to make it work, nor how to make the color dependant of the values.

import numpy as np

from bokeh.plotting import figure, show, output_file #outils graphiques

import pandas as pd
df = pd.read_excel("C:/Users/a873469/Documents/Bundles/Bundles mini.xlsx")
pd.set_option('display.height', 500)
pd.set_option('display.max_rows', 500)
pd.set_option('display.max_columns', 500)
pd.set_option('display.width', 200)
print df.iloc[:20, :20]

options=

colormap = ["#2166ac", "#67a9cf", "#d1e5f0", "#fddbc7", "#ef8a62", "#b2182b"]
xname = []
yname = []
color = []


color.append(colormap)

p = figure(title="Bundles", #table title 
           x_axis_location="above", # x axis on top, hover tool
           x_range=list(reversed(options)), y_range=options) # definition of x et y

p.plot_width = 800 #table size
p.plot_height = 800
p.grid.grid_line_color = None # uncolored grid
p.axis.axis_line_color = None # uncolored axis
p.axis.major_tick_line_color = None #no tick lines on axis
p.axis.major_label_text_font_size = "5pt" #label police
p.axis.major_label_standoff = 0 
p.xaxis.major_label_orientation = np.pi/3 #label orientation 

p.rect(0.9, 0.9, #rectangles 
       color='colors', line_color=None) # rectangle color

output_file("bundles.html", title="bundles.py example") # fichier d'affichage

show(p) # show the plot'
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
R.MARTIN
  • 3
  • 1
  • 8

2 Answers2

3

Edit: Kacper Wolkowski's hint on Pandas capabilities is certainly the better way of proceeding here. I'll still leave the answer.

Not sure what you are doing this for, but a first quick solution (approaching the desired example) would be to plot the matrix of values using the imshow() function of matplotlib and add the values in the squares.

import numpy as np
import matplotlib.pyplot as plt

# generate some example data
matrix = np.random.uniform(0,1,(5,5))

# plot the matrix as an image with an appropriate colormap
plt.imshow(matrix.T, aspect='auto', cmap="bwr")

# add the values
for (i, j), value in np.ndenumerate(matrix):
    plt.text(i, j, "%.3f"%value, va='center', ha='center')

plt.axis('off')
plt.show()

enter image description here

LUB
  • 141
  • 4
0

So I was trying to convert sample df to have colors this way:

df = pd.DataFrame([[1.0, 0.76, 0.71, 0.39],
              [0.76, 0.76, 0.68, 0.38],
              [0.71, 0.68, 0.71, 0.39],
              [0.39, 0.38, 0.39, 0.39],
              [0.36, 0.32, 0.34, 0.20]], columns=['Compass', "GPS","radar","ADF"])

cm = sns.light_palette('green', as_cmap=True)

s = df.style.background_gradient(cmap=cm, low=0, high=1, axis=0)
s

But it works only "per axis" so you need to select if you want to show highest in a row or in a column, and the result looks like that:

enter image description here

You can see that it only shows colors per column.

Unfortunatelly Pandas doesn't support grading the whole df but thanks to this guys ( pandas style background gradient both rows and colums ) it's actually possible to achieve it anyway:

import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import colors

def background_gradient(s, m, M, cmap='PuBu', low=0, high=0):
    rng = M - m
    norm = colors.Normalize(m - (rng * low),
                            M + (rng * high))
    normed = norm(s.values)
    c = [colors.rgb2hex(x) for x in plt.cm.get_cmap(cmap)(normed)]
    return ['background-color: %s' % color for color in c]

df.style.apply(background_gradient,
               cmap='PuBu',
               m=df.min().min(),
               M=df.max().max(),
               low=0,
               high=0.2)

And the final result looks much nicer:

enter image description here

Is that what you were looking for?

Kacper Wolkowski
  • 1,517
  • 1
  • 16
  • 24
  • This seems to work but breaks down when i use a custom diverging colormap, while trying to center at 0....it is unable to ensure that say, all positive numbers are blue and all negative numbers are red on a red --> blue diverging scale. – David Yang Jan 24 '18 at 22:47