如何使用seaborn FacetGrid更改字体大小?

jam*_*don 38 python seaborn

我用factorplotin seaborn和get facetgrid对象绘制了我的数据,但仍无法理解如何在这样的情节中设置以下属性:

  1. 图例大小:当我绘制大量变量时,我会得到非常小的图例,字体很小.
  2. y和x标签的字体大小(与上面类似的问题)

Pau*_*l H 83

您可以扩大通话中的字体sns.set().

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
x = np.random.normal(size=37)
y = np.random.lognormal(size=37)

# defaults
sns.set()
fig, ax = plt.subplots()
ax.plot(x, y, marker='s', linestyle='none', label='small')
ax.legend(loc='upper left', bbox_to_anchor=(0, 1.1))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

sns.set(font_scale=5)  # crazy big
fig, ax = plt.subplots()
ax.plot(x, y, marker='s', linestyle='none', label='big')
ax.legend(loc='upper left', bbox_to_anchor=(0, 1.3))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • 这似乎覆盖了所选的主题。 (7认同)
  • 这很糟糕,因为它完全改变了图形布局,而不仅仅是字体。 (2认同)

ach*_*nnu 24

FacetGrid图确实产生了非常小的标签.虽然@ paul-h已经描述了使用sns.set作为改变字体缩放的方法,但它可能不是最佳解决方案,因为它将改变font_scale所有绘图的设置.

您可以使用它seaborn.plotting_context来更改当前图表的设置:

with sns.plotting_context(font_scale=1.5):
    sns.factorplot(x, y ...)
Run Code Online (Sandbox Code Playgroud)

  • @mwaskom说"如果上下文是'None`,``font_scale`会被忽略,所以你想要`plotting_context("notebook",font_scale = 5.5)`或类似的东西." (http://stackoverflow.com/a/31865996/1135316) (17认同)

Ser*_*obo 14

我对@paul-H 代码做了一些小的修改,这样你就可以独立设置 x/y 轴和图例的字体大小。希望能帮助到你:

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
x = np.random.normal(size=37)
y = np.random.lognormal(size=37)

# defaults                                                                                                         
sns.set()
fig, ax = plt.subplots()
ax.plot(x, y, marker='s', linestyle='none', label='small')
ax.legend(loc='upper left', fontsize=20,bbox_to_anchor=(0, 1.1))
ax.set_xlabel('X_axi',fontsize=20);
ax.set_ylabel('Y_axis',fontsize=20);

plt.show()
Run Code Online (Sandbox Code Playgroud)

这是输出:

在此处输入图片说明


Hie*_*nga 7

对于图例,您可以使用此

plt.setp(g._legend.get_title(), fontsize=20)
Run Code Online (Sandbox Code Playgroud)

其中 g 是调用创建它的函数后返回的 facetgrid 对象。