【发布时间】:2016-01-01 00:50:09
【问题描述】:
我们如何获得 seaborn FacetGrid 热图的图例? .add_legend() 方法对我不起作用。
使用来自this previous question的代码:
import pandas as pd
import numpy as np
import itertools
import seaborn as sns
print("seaborn version {}".format(sns.__version__))
# R expand.grid() function in Python
# https://stackoverflow.com/a/12131385/1135316
def expandgrid(*itrs):
product = list(itertools.product(*itrs))
return {'Var{}'.format(i+1):[x[i] for x in product] for i in range(len(itrs))}
methods=['method 1', 'method2', 'method 3', 'method 4']
times = range(0,100,10)
data = pd.DataFrame(expandgrid(methods, times, times))
data.columns = ['method', 'dtsi','rtsi']
data['nw_score'] = np.random.sample(data.shape[0])
def facet(data,color):
data = data.pivot(index="dtsi", columns='rtsi', values='nw_score')
g = sns.heatmap(data, cmap='Blues', cbar=False)
with sns.plotting_context(font_scale=5.5):
g = sns.FacetGrid(data, col="method", col_wrap=2, size=3, aspect=1)
g = g.map_dataframe(facet)
g.add_legend()
g.set_titles(col_template="{col_name}", fontweight='bold', fontsize=18)
【问题讨论】:
-
你能解释一下你想要什么样的图例/图例吗?当您在一组轴中有多个艺术家时,图例很有用。在您的示例中,每个子图只有一个艺术家(热图),并且每个子图都有唯一的标题,并带有相应的列名,因此在我看来,在这种情况下,图例将是多余的。
-
您需要调整答案here
-
谢谢@mwaskom!我是新手,很想使用它。
标签: python numpy plot heatmap seaborn