2015-12-26 58 views
3

我一直與seaborn及其heatmap功能。我想從大熊貓數據框中df帶有加註解的價值觀來建立矩陣:熊貓seaborn - 熱圖無顏色

C,L,N 
a,x,10 
a,y,2 
a,z,4 
b,x,1 
b,y,22 
b,z,11 
c,x,3 
c,y,1 
c,z,0 

到目前爲止,它工作得很好用:

# Read DataBase 
df = pd.read_csv('myfile.csv') 

# Save Users/City/Language Matrix 
pdf = df.pivot(index='C',columns='L',values='N').fillna(0) 

# Set Font Parameters 
rc = {'font.size': 8, 'xtick.labelsize': 11, 'ytick.labelsize': 11} 

# Set Figure 
fig = plt.figure(figsize=(20, 9)) 

# Assign to Seaborn 
sns.set(rc=rc) 

with sns.axes_style('white'): 

    sns.heatmap(pdf, 
       cbar=False, 
       square=False, 
       annot=True, 
       cmap='Blues', 
       fmt='g', 
       linewidths=0.5) 

將返回:

enter image description here

在結束我有興趣只保留值並將結構保存爲一個簡單的表格,丟棄顏色。我試圖設置cmap=None,但它不起作用,並且沒有cmap,seaborn會將默認cmap分配給熱圖。

from matplotlib.colors import ListedColormap 

with sns.axes_style('white'): 
    sns.heatmap(pdf, 
       cbar=False, 
       square=False, 
       annot=True, 
       fmt='g', 
       cmap=ListedColormap(['white']), 
       linewidths=0.5) 

這將產生:

+0

呃..我不知道如果我沒有正確理解你......是你想要的只是一個表? –

回答

5

如果我不明白你錯了,你想要的是忽略了顏色表,您可以與您選擇的背景色使用matplotlib的ListedColormap創建自定義顏色表:

enter image description here

與STR,十六進制或RGB顏色設置您所選擇的背景顏色替換字符串white

不過,我相信pandas提供了更好的導出選項。找到波紋管的所有可能的輸出選項的屏幕:

enter image description here

根據要插入表格,也許to_htmlto_jsonto_latex比與seaborn策劃更好的選擇。