Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?". 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)

enter image description here

like image 215
Chris Warth Avatar asked Feb 10 '23 09:02

Chris Warth


1 Answers

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?
like image 50
Chris Warth Avatar answered Feb 12 '23 01:02

Chris Warth