【问题标题】:change font size of facet titles using seaborn facetgrid heatmap使用 seaborn facetgrid 热图更改构面标题的字体大小
【发布时间】:2015-08-06 20:02:37
【问题描述】:

注意:这是与“How can I change the font size using seaborn FacetGrid?”不同的问题。在 facetgrid 中使用热图时,建议的方法不起作用。

在 facetgrid 中绘制热图时,如何更改构面标题的字体大小?

下面的代码尝试了两种方法,将fontsize= 传递给set_titles() 并将整个内容包装在绘图上下文中。据我所知,在使用热图时,两者似乎都对构面标题没有任何影响,尽管字体粗细确实发生了变化。使用热图时是否还有其他选项可以控制构面标题?

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)

【问题讨论】:

  • 我认为你想将size= 传递给set_titles,而不是fontsize=

标签: python matplotlib seaborn


【解决方案1】:

感谢@mwaskon,这就是答案 - 在调用 set_titles 时使用 size=

这会导致更多的问题,比如

  • 能否请您更改 set_titles 使用 fontweight=fontsize= 而不是 fontweight=size=? size= 在别处用于以英寸为单位的刻面高度。
  • 为什么sns.plotting_context在这方面完全无效 上下文?

【讨论】:

  • set_titles 只是将额外的关键字参数传递给 matplotlib 方法 ax.set_title,这需要 size,所以 seaborn 对此无能为力。至于plotting_context,由于它的工作方式,如果contextNonefont_scale 将被忽略,所以你想要plotting_context("notebook", font_scale=5.5) 或类似的。
  • 再次感谢,我收到了来自hereplotting_context() 建议。我会在那里发表评论,指出您的答案 - 可能会减轻一些悲伤。
猜你喜欢
  • 2014-10-09
  • 1970-01-01
  • 1970-01-01
  • 2016-12-19
  • 1970-01-01
  • 2016-01-25
  • 2015-06-17
  • 2019-01-23
  • 1970-01-01
相关资源
最近更新 更多