2015-12-26 61 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策划更好的选择。