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

2024-04-20 09:01:44 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

Tags: 数据对象绘图属性绘制字体标签seaborn
3条回答

您可以将调用中的字体放大到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

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

您可以使用^{}更改当前绘图的设置:

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

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

相关问题 更多 >