如何在 Python Seaborn 包中更改图形的大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33446029/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change a figure's size in Python Seaborn package
提问by Vikram Josyula
I'm having trouble increasing the size of my plot figures using Seaborn (imported as sns
). I'm using sns.pairplot
to plot columns of a data frame against one another.
我在使用 Seaborn(导入为sns
)增加绘图数字的大小时遇到了麻烦。我正在使用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. I've tried a few different suggestions from online boards, but nothing seems to work.
我以为plot.rCParams['figure.figsize']
会控制数字有多大,但它似乎没有生效。我尝试了一些来自在线论坛的不同建议,但似乎没有任何效果。
回答by S.Zuo
Try to put the size in parenthesis, this does the trick for me:
尝试将大小放在括号中,这对我有用:
plt.rcParams['figure.figsize']=(10,10)
回答by Martin Alexandersson
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)
回答by dejanmarich
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.
不工作(情节非常小)。为了在 jupyter 中显示绘图,在plt.figure(figsize=(25,20))
之前添加行sns.boxplot()
并包含%matplotlib inline
当然很重要。
回答by ImportanceOfBeingErnest
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.
除了@MartinAnderson 的有效答案之外,seaborn 本身还提供了设置网格子图高度的选项。结合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功能,如
pairplot
,relplot
, catplot
,lmplot
和底层PairGrid
或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.
对于其他直接绘制到坐标轴的 seaborn 图,您如何更改使用 matplotlib 绘制的图形的大小?会正常工作。
回答by Jeevan
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)
回答by Julio Ludovic Ramihone
You can use set for style control : https://seaborn.pydata.org/generated/seaborn.set.html
您可以使用 set 进行样式控制:https: //seaborn.pydata.org/generated/seaborn.set.html
sns.set(rc={'figure.figsize':(20,10)})