Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从绘图中获取热图注释字典_Python_Seaborn - Fatal编程技术网

Python 从绘图中获取热图注释字典

Python 从绘图中获取热图注释字典,python,seaborn,Python,Seaborn,我有一张热图: fig=figsize(8,8) ax=sbn.heatmap(好,annot=True,fmt='.2f',线宽=.3,annot_-kws={“大小”:14},square=True,robust=True,cmap=sbn.light_调色板((210,90,60),input=“husl”)) seaborn热图方便地设置了我注释的颜色。我想访问annot\u kws字典,但我不知道如何访问。我基本上希望在不同的绘图中重用seaborn自动生成的颜色 更清楚的例子:

我有一张热图:

fig=figsize(8,8)
ax=sbn.heatmap(好,annot=True,fmt='.2f',线宽=.3,annot_-kws={“大小”:14},square=True,robust=True,cmap=sbn.light_调色板((210,90,60),input=“husl”))

seaborn热图方便地设置了我注释的颜色。我想访问
annot\u kws
字典,但我不知道如何访问。我基本上希望在不同的绘图中重用seaborn自动生成的颜色

更清楚的例子:

test=np.array([np.array([0.77,0.21]),np.array([0.21,0.51]))
ax=sbn.heatmap(测试,annot=True,fmt='.2f',annot_-kws={“大小”:14},cmap=sbn.light_调色板((210,90,60),input=“husl”))
给我

我可以将默认注释的颜色全部更改为单一颜色

test=np.array([np.array([0.77,0.21]),np.array([0.21,0.51]))
ax=sbn.heatmap(测试,annot=True,fmt='.2f',annot_-kws={“大小”:14,“颜色”:“黑色”},cmap=sbn.light_调色板((210,90,60),input=“husl”))
这让我


我想将信息传递给热图,即让我将所有白色注释更改为黄色,但将黑色注释保留为黑色。我想如果我能得到关于当前注释颜色的信息,我可以根据不同颜色的黑色或白色来更新它们,但我不知道如何获得这些信息

编辑:我第一次误解了你的问题。我认为这个经过编辑的答案符合你的要求

您可以通过
子批访问注释(即对象)。获取子批()

代码:

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())
28
14.0
'black'
12.0
'black' # this is a coincidence
输出:

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())
28
14.0
'black'
12.0
'black' # this is a coincidence

一旦我有了这些文本对象,我想再次设置它们,比如说,使用不同的颜色,但只有颜色为“黑色”的obj 1和其他颜色不同的所有对象查看我在回答中链接的文本文档。get和set方法是成对的。非常好!非常感谢。我忽略了这一点。