2017-07-07 47 views
-1

我有蟒蛇如下表:什麼碼錶顏色而不是值

Image

我想要做的就是看錶,一個更好看的方式,與細胞從去紅色到藍色取決於它們的價值,有點像這樣:

RdBl

有誰知道該怎麼做?

到目前爲止,我已經完成了這項工作,但不知道要在變量選項中放置什麼以使其工作,也不知道如何使顏色取決於值。

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' 
+0

你應該閱讀這個,它可能會幫助http://pandas.pydata.org/pandas-docs/stable/style.html –

+0

謝謝,我會看到我設法做的 –

回答

0

所以我想轉換樣本DF有顏色是這樣的:

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 

但它只能「按軸」,所以你需要選擇,如果你想顯示最高的一排或一列,結果如下所示:

enter image description here

您可以看到它只顯示每列的顏色。

可惜的是熊貓不支持分級的整體DF但由於這個傢伙(pandas style background gradient both rows and colums),它實際上可能無論如何要實現它:

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) 

和最終結果看上去更漂亮:

enter image description here

這是你在找什麼?

+0

這似乎工作,但分解當我嘗試以0爲中心時,使用自定義發散色彩地圖...無法確保所有正數都是藍色,並且所有負數在紅色 - >藍色分化比例上都是紅色。 –

1

編輯:Kacper Wolkowski對熊貓能力的暗示肯定是在這裏繼續的更好的方式。我仍然會留下答案。

不確定你在做什麼,但第一個快速解決方案(接近所需示例)將使用matplotlibimshow()函數繪製值矩陣,並將這些值添加到正方形中。

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

相關問題