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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25328003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:03:23  来源:igfitidea点击:

How can I change the font size using seaborn FacetGrid?

pythonseaborn

提问by james pardon

I have plotted my data with factorplotin seabornand get facetgridobject, but still cannot understand how the following attributes could be set in such a plot:

我已经用factorplotinseaborn和 getfacetgrid对象绘制了我的数据,但仍然无法理解如何在这样的图中设置以下属性:

  1. Legend size: when I plot lots of variables, I get very small legends, with small fonts.
  2. Font sizes of y and x labels (a similar problem as above)
  1. 图例大小:当我绘制大量变量时,我会得到非常小的图例,带有小字体。
  2. y 和 x 标签的字体大小(与上面类似的问题)

采纳答案by Paul H

You can scale up the fonts in your call to sns.set().

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

在此处输入图片说明

回答by achennu

The FacetGridplot does produce pretty small labels. While @paul-h has described the use of sns.setas a way to the change the font scaling, it may not be the optimal solution since it will change the font_scalesetting for all plots.

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

You could use the seaborn.plotting_contextto change the settings for just the current plot:

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

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

回答by Sergio Cobo

I've made small modifications to @paul-H code, such that you can set the font size for the x/y axes and legend independently. Hope it helps:

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

This is the output:

这是输出:

enter image description here

在此处输入图片说明

回答by Hielke Walinga

For the legend, you can use this

对于图例,您可以使用此

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

Where g is your facetgrid object returned after you call the function making it.

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