我如何改变我的图像的大小,使其适合打印?

例如,我想使用A4纸,它的尺寸是11.7英寸乘8.27英寸。


当前回答

首先导入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)

其他回答

# Sets the figure size temporarily but has to be set again the next plot
plt.figure(figsize=(18,18))

sns.barplot(x=housing.ocean_proximity, y=housing.median_house_value)
plt.show()

对于我的图(一个sns因子图),建议的答案并不能很好地工作。

因此我使用

plt.gcf().set_size_inches(11.7, 8.27)

就在与seaborn的情节之后(所以不需要传递一个斧头给seaborn或改变rc设置)。

除了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)

请注意,如果您试图传递给seaborn中的“图形级”方法(例如lmplot, catplot / factorplot, jointplot),您可以并且应该在参数中使用height和aspect指定此方法。

sns.catplot(data=df, x='xvar', y='yvar', 
    hue='hue_bar', height=8.27, aspect=11.7/8.27)

请参阅https://github.com/mwaskom/seaborn/issues/488和使用matplotlib面向对象接口使用seaborn进行绘图,以获得关于图形级方法不遵守轴规范的更多详细信息。

首先导入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)