【问题标题】:pandas and seaborn - heatmap with no colorspandas 和 seaborn - 没有颜色的热图
【发布时间】:2015-12-26 11:36:50
【问题描述】:

我一直在使用seaborn 及其heatmap 函数。我想用 pandas 数据框 df 的注释值构建一个矩阵:

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)

返回:

最后我有兴趣只保留值并将结构保存为一个简单的表格,丢弃颜色。我尝试设置cmap=None,但它不起作用,并且没有cmap,seaborn 将默认cmap 分配给热图。

【问题讨论】:

  • 嗯..我不知道我是否正确理解你......你想要的只是一张桌子吗?

标签: python pandas seaborn


【解决方案1】:

如果我没有理解你的错误,而你只想忽略颜色图,你可以使用 matplotlib 的ListedColormap 使用你选择的背景颜色创建自定义颜色图:

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)

这将产生:

将字符串 white 替换为 STR、HEX 或 RGB 颜色以设置您选择的背景颜色。

但是,我相信pandas 提供了更好的导出选项。在下面找到所有可能的导出选项的屏幕:

根据您要插入表格的位置,to_htmlto_jsonto_latex 可能是比使用 seaborn 绘图更好的选择。

【讨论】:

    猜你喜欢
    • 2018-05-09
    • 1970-01-01
    • 1970-01-01
    • 2020-10-21
    • 2021-05-19
    • 2018-05-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多