[英]Get heatmap annotation dictionary from plot
I have a heatmap:我有一个热图:
fig =figsize(8,8)
ax = sbn.heatmap(good,annot=True, fmt='.2f', linewidths=.3, annot_kws={"size": 14},square=True,robust=True,cmap=sbn.light_palette((210, 90, 60), input="husl") )
The seaborn heatmap conveniently set colors of my annotations. seaborn 热图可以方便地设置我的注释的颜色。 I would like to access the
annot_kws
dictionary, but I have no idea how to do so.我想访问
annot_kws
字典,但我不知道如何访问。 I essentially want to reused the auto generated colors by seaborn
in a different plot.我基本上想在不同的情节中重用由
seaborn
自动生成的颜色。
Clearer example:更清晰的例子:
test = np.array([np.array([0.77,0.21]),np.array([0.21,0.51])])
ax = sbn.heatmap(test,annot=True, fmt='.2f', annot_kws={"size": 14},cmap=sbn.light_palette((210, 90, 60), input="husl") )
I can change the color of the default annotation to all a single color我可以将默认注释的颜色更改为所有单一颜色
test = np.array([np.array([0.77,0.21]),np.array([0.21,0.51])])
ax = sbn.heatmap(test,annot=True, fmt='.2f', annot_kws={"size": 14, "color":'black'},cmap=sbn.light_palette((210, 90, 60), input="husl") )
Which gives me this picture这给了我这张照片
I would like to pass information to the heatmap, that say lets me change all the white annotations to yellow, but leave the black ones black.我想将信息传递给热图,也就是说让我将所有白色注释更改为黄色,但将黑色注释保留为黑色。 And I thought if I can get information on the current annotation colors I could update them based on whether it is black or white with a different color, but have no idea how to actually get that information.
我想如果我能获得有关当前注释颜色的信息,我可以根据它是黑色还是具有不同颜色的白色来更新它们,但不知道如何实际获取该信息。
EDIT: I misread your question the first time.编辑:我第一次误读了你的问题。 I think this edited answer gets at what you were asking.
我认为这个编辑过的答案符合您的要求。
You can access the annotations (ie, Text
objects) through subplots.get_children()
您可以通过
subplots.get_children()
访问注释(即Text
对象subplots.get_children()
Code:代码:
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
from matplotlib.text import Text
# default colormap example
df = pd.DataFrame(np.random.normal(size=(5, 5)))
subplots = sns.heatmap(df.corr(), annot=True, annot_kws={"size": 14, "color": "black"})
# the first 5 * 5 Text objects in text_objs are the matrix annotations
# the few at the end are default annotations (title text I think) and are not
# formatted according to to annot_kws; ignore these
text_objs = list(filter(lambda x: isinstance(x, Text), subplots.get_children()))
print(len(text_objs))
# first Text object
print(text_objs[0].get_size())
print(text_objs[0].get_color())
# last Text object
print(text_objs[-1].get_size())
print(text_objs[-1].get_color())
Output:输出:
28
14.0
'black'
12.0
'black' # this is a coincidence
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.