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

时间:2014-08-15 14:12:39

标签: python seaborn

我已在factorplot中使用seaborn绘制了我的数据并获取了facetgrid个对象,但仍然无法理解如何在这样的情节中设置以下属性:

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

4 个答案:

答案 0 :(得分:76)

您可以将通话中的字体向上扩展为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))

enter image description here

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

enter image description here

答案 1 :(得分:21)

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

您可以使用seaborn.plotting_context更改当前情节的设置:

with sns.plotting_context(font_scale=1.5):
    sns.factorplot(x, y ...)

答案 2 :(得分:3)

我对@ 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()

这是输出:

enter image description here

答案 3 :(得分:1)

对于图例,您可以使用

plt.setp(g._legend.get_title(), fontsize=20)

在哪里g是调用构造函数后返回的facetgrid对象。

相关问题