简体   繁体   English

使用seaborn facetgrid heatmap更改构面标题的字体大小

[英]change font size of facet titles using seaborn facetgrid heatmap

Note: this is a different question than " How can I change the font size using seaborn FacetGrid? ". 注意:这是与“ 如何使用seaborn FacetGrid更改字体大小? ”不同的问题。 The methods suggested there do not work when using a heatmap inside a facetgrid. 在构面网格内使用热图时,此处建议的方法不起作用。

How can I change the font size of the facet titles when plotting heatmaps inside a facetgrid? 在构面网格内绘制热图时,如何更改构面标题的字体大小?

The code below tries two methods, passing fontsize= to set_titles() and wrapping the whole thing in a plotting context. 下面的代码尝试了两种方法,将fontsize=传递给set_titles()并将整个内容包装在绘图上下文中。 As far as I can tell, neither seems to have any effect on facet titles when using heatmap, although the fontweight did change. 据我所知,尽管字体重量确实发生了变化,但是在使用热图时,似乎都没有影响面标题。 Are there any other options for controlling facet title when using heatmap? 使用热图时,是否还有其他选项可以控制构面标题?

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.set_titles(col_template="{col_name}", fontweight='bold', fontsize=18)

在此处输入图片说明

Thanks @mwaskon, that is the answer - use size= when called set_titles . 感谢@mwaskon,这就是答案-在调用set_titles时使用size=

That leads to more questions, like 这导致了更多的问题,例如

  • Can you please change set_titles used fontweight= and fontsize= instead of fontweight= and size= ? 能否请您更改set_titles使用fontweight=fontsize=代替fontweight=size= size= is use elsewhere for the facet height in inches. size=用于其他方面的刻面高度(以英寸为单位)。
  • why is sns.plotting_context completely ineffective in this context? 为什么sns.plotting_context在这种情况下完全无效?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM