How to change a figure's size in Python Seaborn package

拜拜、爱过 提交于 2019-12-17 16:26:12

问题


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:


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)



回答2:


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

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



回答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 only the width then you 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.



来源:https://stackoverflow.com/questions/33446029/how-to-change-a-figures-size-in-python-seaborn-package

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!