How to change a figure's size in Python Seabor

2019-01-11 17:38发布

问题:

I'm having trouble increasing the size of my plot figures using Seaborn. I'm using sns.pairplot to plot columns of a data frame against one another.

    %matplotlib inline
    plt.rcParams['figure.figsize']=10,10
    columns=list(df.columns.values)
    g=sns.pairplot(df, kind='reg', x_vars=columns,y_vars = ['Column 1'])

The plots populate with data just fine, but the figure size is too small. I thought plot.rCParams['figure.figsize'] would control how large the figure is, but it doesn't seem to take effect. I've tried a few different suggestions from online boards, but nothing seems to work.

回答1:

Try to put the size in parenthesis, this does the trick for me:

plt.rcParams['figure.figsize']=(10,10)


回答2:

sns.pairplot "Returns the underlying PairGrid instance for further tweaking" ...for instance changing the figure size:

g=sns.pairplot(df, kind='reg', x_vars=columns,y_vars = ['Column 1'])
g.fig.set_size_inches(15,15)


回答3:

In addition to the well working answer by @MartinAnderson, seaborn itself provides the option to set the height of the subplots of the grid. In combination with the aspect this determines the overall size of the figure in dependence of the number of subplots in the grid.

In seaborn <= 0.8.1:

g = sns.pairplot(..., size=10, aspect=0.6)

In seaborn >= 0.9.0:

g = sns.pairplot(..., height=10, aspect=0.6)

Note that this applies to all seaborn functions which generate a figure level grid, like pairplot, relplot, catplot, lmplot and the underlying PairGrid or FacetGrid.

For other seaborn plots, which directly plot to axes, the solutions from How do you change the size of figures drawn with matplotlib? will work fine.



回答4:

If we would like to change only the height or width then we can do

g = sns.pairplot(df, kind='reg', x_vars=columns,y_vars = ['Column 1'])
g.fig.set_figheight(6)
g.fig.set_figwidth(10)


回答5:

Reffering to Rahul's question about sns.catplot ( Unable to change the plot size with matplotlib and seaborn )

If you try in jupyter notebook:

plt.figure(figsize=(25,20))
sns.boxplot(x='CriticRating', y='AudienceRating', data=movies)

it is working, but

sns.boxplot(x='CriticRating', y='AudienceRating', data=movies)
plt.figure(figsize=(25,20))

is not working (plot is very small). It's important to add line plt.figure(figsize=(25,20)) before sns.boxplot()and include %matplotlib inline of course in order to display plot in jupyter.