pandas 更改seaborn热图中的某些方块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31707033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
change certain squares in a seaborn heatmap
提问by drg
Say I have a heatmap that looks like this (axes are trimmed off): 
I want to be able to alter certain squares to denote statistical significance. I know that I could mask out the squares that are not statistically significant, but I still want to retain that information (and not set the values to zero). Options for doing this include 1) making the text on certain squares bold, 2) adding a hatch-like functionality so that certain squares have stippling, or 3) adding a symbol to certain squares.
我希望能够改变某些平方来表示统计显着性。我知道我可以掩盖统计上不显着的平方,但我仍然想保留这些信息(而不是将值设置为零)。执行此操作的选项包括 1) 使某些方块上的文本加粗,2) 添加类似阴影的功能,以便某些方块具有点画效果,或 3) 向某些方块添加符号。
Any ideas on this? Thanks!
对此有何想法?谢谢!
采纳答案by mwaskom
You could plot twice, applying a mask to the cells you do not want to emphasize the second time:
您可以绘制两次,将掩码应用于您不想第二次强调的单元格:
import numpy as np
import seaborn as sns
x = np.random.randn(10, 10)
sns.heatmap(x, annot=True)
sns.heatmap(x, mask=x < 1, cbar=False,
annot=True, annot_kws={"weight": "bold"})
回答by jme
One approach is to access the Textobjects directly and change their weight/style. The below code will take some sample data and try to make every entry equal to 118 stand out:
一种方法是Text直接访问对象并更改它们的权重/样式。下面的代码将采用一些示例数据,并尝试使每个等于 118 的条目脱颖而出:
flights = sns.load_dataset("flights")
flights = flights.pivot("month", "year", "passengers")
ax = sns.heatmap(flights, annot=True, fmt="d")
for text in ax.texts:
text.set_size(14)
if text.get_text() == '118':
text.set_size(18)
text.set_weight('bold')
text.set_style('italic')
I'm not a matplotlib/seaborn expert, but it appears to me that requiring an individual cell in the heatmap to be hatched would require a bit of work. In short, the heatmap is a Collectionof matplotlibPatches, and the hatching of a collection can only be set on the collection as a whole. To set the hatch of an individual cell, you need them to be distinct patches, and things get messy. Perhaps (hopefully) someone more knowledgeable than I can come along and say that this is wrong, and that it's quite easy -- but if I had to guess, I'd say that changing the text style will be easier than setting a hatch.
我不是 matplotlib/seaborn 专家,但在我看来,需要孵化热图中的单个单元格需要一些工作。总之,热图是Collection的matplotlibPatchES和集合的孵化只能在集合作为一个整体进行设置。要设置单个单元格的孵化,您需要它们是不同的补丁,事情会变得混乱。也许(希望)有人比我更有见识并说这是错误的,而且这很容易——但如果我不得不猜测,我会说更改文本样式比设置阴影更容易。

