change font size of facet titles using seaborn facetgrid heatmap

二次信任 提交于 2019-12-19 09:29:11

问题


Note: this is a different question than "How can I change the font size using 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. 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)


回答1:


Thanks @mwaskon, that is the answer - use size= when called set_titles.

That leads to more questions, like

  • Can you please change set_titles used fontweight= and fontsize= instead of fontweight= and size=? size= is use elsewhere for the facet height in inches.
  • why is sns.plotting_context completely ineffective in this context?


来源:https://stackoverflow.com/questions/31864770/change-font-size-of-facet-titles-using-seaborn-facetgrid-heatmap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!