以通用方式更改 seaborn 图和 matplotlib 库图的大小

changing size of seaborn plots and matplotlib library plots in a common way

from pylab import rcParams
rcParams['figure.figsize'] = (10, 10)

这适用于直方图但不适用于因子图。 sns.factorplot (.....) 仍然显示默认大小。

sns.factorplot('Pclass','Survived',hue='person',data = titanic_df,size = 6,aspect =1)

我每次都必须指定尺寸和长宽比。

请提出对他们双方都适用的全局建议。

无法通过 rcParams 更改 factorplot 的图形大小。

图形尺寸为hardcoded inside the FacetGrid class

figsize = (ncol * size * aspect, nrow * size)

然后使用此 figsize 创建一个新图形。

这使得除了 factorplot 函数调用中的参数之外,无法通过其他方式更改图形大小。这也使得无法首先创建具有其他参数的图形并将因子图绘制到该图形。但是,对于单轴因子图的解决方法,请参见 .

seaborn 的作者 argues here 认为这是因为因子图需要完全控制图形。

虽然有人可能会质疑是否需要这样,但您对此无能为力,除了 (a) 在 GitHub site and/or 处添加功能请求并编写您自己的包装器 - 这不会太难,因为 seaborn 和 matplotlib 都是开源的。

可以通过首先创建图形和轴并将其作为参数传递给 seaborn 图来修改图形大小:

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns       

fig, ax = plt.subplots(figsize=(10, 10))

df = pd.DataFrame({
    'product' : ['A', 'A', 'A', 'B', 'B', 'C', 'C'], 
    'close' : [1, 1, 0, 1, 1, 1, 0],
    'counts' : [3, 3, 3, 2, 2, 2, 2]})

sns.factorplot(y='counts', x='product', hue='close', data=df, kind='bar', palette='muted', ax=ax)
plt.close(2)    # close empty figure
plt.show()

当使用 Axis grids 类型的绘图时,seaborn 会自动创建另一个图形。解决方法是关闭空的第二个数字。