首页 > 解决方案 > plt.figure 不调整图形大小

问题描述

我有这个功能,我想改变图形的大小,但是无论我在 figsize 中输入什么值,图形大小都不会改变。任何人都可以提出什么问题吗?或者是否有其他方法可以做到这一点?

def plot_comparison_barplots(df, bar_col, hue_col, scaled):

    fig = plt.figure(figsize=(12,10))
    fig.set_tight_layout(True)
    plt.rcParams.update({'font.size': 9})
     
    ax = sns.catplot(x=bar_col, y='count in %', data=df, hue=hue_col, kind='bar', order=scaled) 

标签: pythonmatplotlibplotseabornfigsize

解决方案


有一种更简单的方法来实现此功能,使用subplots代替figure,当您使用该set_tight_layout()方法时,这会自动更改轴的位置以使您的图形看起来更好,并确保没有任何轴名称被截断。解决此问题的最快方法是在方法之前定义轴set_tight_layout(),以便figsize参数覆盖它。

改变

fig = plt.figure(figsize=(12,10))

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


推荐阅读