简体   繁体   English

如何在 Python Seaborn 包中更改图形的大小

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

I'm having trouble increasing the size of my plot figures using Seaborn (imported as sns ).我在使用 Seaborn (导入为sns )增加我的情节人物的大小时遇到​​了麻烦。 I'm using sns.pairplot to plot columns of a data frame against one another.我正在使用sns.pairplot将数据框的列相互绘制。

    %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.我以为plot.rCParams['figure.figsize']会控制这个数字有多大,但它似乎没有生效。 I've tried a few different suggestions from online boards, but nothing seems to work.我尝试了一些来自在线板的不同建议,但似乎没有任何效果。

sns.pairplot "Returns the underlying PairGrid instance for further tweaking" ...for instance changing the figure size: sns.pairplot “返回底层 PairGrid 实例以进行进一步调整” ...例如更改图形大小:

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

尝试将大小放在括号中,这对我有用:

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

In addition to the well working answer by @MartinAnderson , seaborn itself provides the option to set the height of the subplots of the grid.除了@MartinAnderson 的出色回答之外,seaborn 本身还提供了设置网格子图高度的选项。 In combination with the aspect this determines the overall size of the figure in dependence of the number of subplots in the grid.结合aspect ,这决定了图形的整体大小,具体取决于网格中子图的数量。

In seaborn <= 0.8.1:在seaborn <= 0.8.1:

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

In seaborn >= 0.9.0:在 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 .请注意,这适用于所有生成图形级别网格的 seaborn 函数,例如pairplotrelplotcatplotlmplot和底层的PairGridFacetGrid

For other seaborn plots, which directly plot to axes, the solutions from How do you change the size of figures drawn with matplotlib?对于直接绘制到轴的其他 seaborn 图,解决方案来自如何更改使用 matplotlib 绘制的图形的大小? will work fine.会正常工作。

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)

您可以使用 set 进行样式控制: https ://seaborn.pydata.org/generated/seaborn.set.html

sns.set(rc={'figure.figsize':(20,10)})

Reffering to Rahul's question about sns.catplot ( Unable to change the plot size with matplotlib and seaborn )参考 Rahul 关于 sns.catplot 的问题( 无法使用 matplotlib 和 seaborn 更改绘图大小

If you try in jupyter notebook:如果您在 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.在 sns.boxplot( plt.figure(figsize=(25,20)) sns.boxplot()并包括%matplotlib inline当然是很重要的,以便在 jupyter 中显示绘图。

You can use set for style control : https://seaborn.pydata.org/generated/seaborn.set.html您可以使用 set 进行样式控制: https ://seaborn.pydata.org/generated/seaborn.set.html

We can accomplish this using sns.set() For example:我们可以使用sns.set()来完成此操作,例如:

sns.set(rc={'figure.figsize':(20,10)})

Though, the preferred syntax is:不过,首选语法是:

sns.set_theme()

NB: sns.set() or sns.set_theme() is a high level command that will set the (parameters) for everything else in your notebook.注意: sns.set()sns.set_theme()是一个高级命令,它将为笔记本中的其他所有内容设置(参数)。

sns.set(rc={'figure.figsize':(20,10)}) will set this to be the figure size for everything else in your notebook sns.set(rc={'figure.figsize':(20,10)})将其设置为笔记本中其他所有内容的图形大小

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM