Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

python - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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?

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...