Python 如何更改seaborn图的图形大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31594549/
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 do I change the figure size for a seaborn plot?
提问by Michael Grazebrook
How do I change the size of my image so it's suitable for printing?
如何更改图像的大小以使其适合打印?
For example, I'd like to use to A4 paper, whose dimensions are 11.7 inches by 8.27 inches in landscape orientation.
例如,我想使用 A4 纸,横向尺寸为 11.7 英寸 x 8.27 英寸。
采纳答案by Paul H
You need to create the matplotlib Figure and Axes objects ahead of time, specifying how big the figure is:
您需要提前创建 matplotlib Figure 和 Axes 对象,指定图形有多大:
from matplotlib import pyplot
import seaborn
import mylib
a4_dims = (11.7, 8.27)
df = mylib.load_data()
fig, ax = pyplot.subplots(figsize=a4_dims)
seaborn.violinplot(ax=ax, data=df, **violin_options)
回答by Jianxun Li
You can set the context to be posteror manually set fig_size.
您可以将上下文设置为poster或手动设置fig_size。
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
np.random.seed(0)
n, p = 40, 8
d = np.random.normal(0, 2, (n, p))
d += np.log(np.arange(1, p + 1)) * -5 + 10
# plot
sns.set_style('ticks')
fig, ax = plt.subplots()
# the size of A4 paper
fig.set_size_inches(11.7, 8.27)
sns.violinplot(data=d, inner="points", ax=ax)
sns.despine()
fig.savefig('example.png')
回答by student
You can also set figure size by passing dictionary to rcparameter with key 'figure.figsize'in seaborn setmethod:
您还可以通过在 seaborn方法中rc使用键将字典传递给参数来设置图形大小:'figure.figsize'set
import seaborn as sns
sns.set(rc={'figure.figsize':(11.7,8.27)})
Other alternative may be to use figure.figsizeof rcParamsto set figure size as below:
其他替代可以是使用figure.figsize的rcParams对集合的数字大小如下:
from matplotlib import rcParams
# figure size in inches
rcParams['figure.figsize'] = 11.7,8.27
More details can be found in matplotlib documentation
更多细节可以在matplotlib 文档中找到
回答by Gucci148
first import matplotlib and use it to set the size of the figure
首先导入matplotlib并使用它来设置图形的大小
from matplotlib import pyplot as plt
import seaborn as sns
plt.figure(figsize=(15,8))
ax = sns.barplot(x="Word", y="Frequency", data=boxdata)
回答by elz
Note that if you are trying to pass to a "figure level" method in seaborn (for example lmplot, catplot/ factorplot, jointplot) you can and should specify this within the arguments using heightand aspect.
请注意,如果您尝试传递给 seaborn 中的“图形级别”方法(例如lmplot, catplot/ factorplot, jointplot),您可以并且应该使用heightand在参数中指定它aspect。
sns.catplot(data=df, x='xvar', y='yvar',
hue='hue_bar', height=8.27, aspect=11.7/8.27)
See https://github.com/mwaskom/seaborn/issues/488and Plotting with seaborn using the matplotlib object-oriented interfacefor more details on the fact that figure level methods do not obey axes specifications.
请参阅https://github.com/mwaskom/seaborn/issues/488和使用 matplotlib 面向对象的接口使用 seaborn 绘图,了解有关图形级别方法不遵守轴规范这一事实的更多详细信息。
回答by Chris
The top answers by Paul H and J. Li do not work for all types of seaborn figures. For the FacetGridtype (for instance sns.lmplot()), use the sizeand aspectparameter.
Paul H 和 J. Li 的最佳答案不适用于所有类型的海生人物。对于FacetGrid类型(例如sns.lmplot()),使用sizeandaspect参数。
Sizechanges both the height and width, maintaining the aspect ratio.
Size改变高度和宽度,保持纵横比。
Aspectonly changes the width, keeping the height constant.
Aspect只改变宽度,保持高度不变。
You can always get your desired size by playing with these two parameters.
您始终可以通过使用这两个参数来获得所需的尺寸。
回答by head7
For my plot (a sns factorplot) the proposed answer didn't works fine.
对于我的情节(sns factorplot),建议的答案不起作用。
Thus I use
因此我使用
plt.gcf().set_size_inches(11.7, 8.27)
Just after the plot with seaborn (so no need to pass an ax to seaborn or to change the rc settings).
就在与 seaborn 的情节之后(因此无需将 ax 传递给 seaborn 或更改 rc 设置)。
回答by Mufaddal Tahir
This shall also work.
这也将起作用。
from matplotlib import pyplot as plt
import seaborn as sns
plt.figure(figsize=(15,16))
sns.countplot(data=yourdata, ...)
回答by dmainz
In addition to elzanswer regarding "figure level" methods that return multi-plot grid objects it is possible to set the figure height and width explicitly (that is without using aspect ratio) using the following approach:
除了有关返回多图网格对象的“图形级别”方法的elz回答之外,还可以使用以下方法显式设置图形高度和宽度(即不使用纵横比):
import seaborn as sns
g = sns.catplot(data=df, x='xvar', y='yvar', hue='hue_bar')
g.fig.set_figwidth(8.27)
g.fig.set_figheight(11.7)
回答by PSN
This can be done using:
这可以使用:
plt.figure(figsize=(15,8))
sns.kdeplot(data,shade=True)


