389

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.

4

13 回答 13

423

您还可以通过使用seaborn方法rc中的键将字典传递给参数来设置图形大小:'figure.figsize'set

import seaborn as sns

sns.set(rc={'figure.figsize':(11.7,8.27)})

其他替代方法可能是用于figure.figsize设置rcParams图形大小,如下所示:

from matplotlib import rcParams

# figure size in inches
rcParams['figure.figsize'] = 11.7,8.27

更多细节可以在matplotlib 文档中找到

于 2017-12-23T20:34:00.930 回答
296

您需要提前创建 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)
于 2015-07-23T20:19:13.877 回答
187

请注意,如果您尝试传递给 seaborn 中的“图形级别”方法(例如/ lmplot、 ) ,您可以并且应该在使用的参数中指定它and 。catplotfactorplotjointplotheightaspect

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 进行绘图。

于 2018-07-30T21:20:11.640 回答
110

首先导入 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)
于 2018-06-03T21:18:01.737 回答
104

您可以将上下文设置为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')

在此处输入图像描述

于 2015-07-23T20:13:37.863 回答
56

这可以使用以下方法完成:

plt.figure(figsize=(15,8))
sns.kdeplot(data,shade=True)
于 2019-08-15T18:57:18.067 回答
53

除了关于返回多图网格对象的“图形级别”方法的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)
于 2019-07-10T12:18:56.260 回答
28

这也应该有效。

from matplotlib import pyplot as plt
import seaborn as sns    

plt.figure(figsize=(15,16))
sns.countplot(data=yourdata, ...)
于 2019-03-17T15:13:20.170 回答
25

对于我的情节(sns factorplot),建议的答案效果不佳。

因此我使用

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

就在与 seaborn 的情节之后(因此无需将斧头传递给 seaborn 或更改 rc 设置)。

于 2018-12-11T14:03:10.073 回答
9
# 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()

我的图表的 18x18 绘图

于 2021-04-23T08:03:45.337 回答
8
  • 调整绘图的大小取决于绘图是像 一样的图形级绘图seaborn.displot,还是像 一样的轴级绘图seaborn.histplot此答案适用于任何图形或轴级别图
  • seaborn是 的高级 API matplotlib,因此 seaborn 使用 matplotlib 方法
  • python 3.8.12, matplotlib 3.4.3,中测试seaborn 0.11.2

导入和数据

import seaborn as sns
import matplotlib.pyplot as plt

# load data
df = sns.load_dataset('penguins')

sns.displot

  • 可以使用height和/或aspect参数调整图形级别图的大小
  • 此外,dpi可以通过访问fig对象并使用.set_dpi()
p = sns.displot(data=df, x='flipper_length_mm', stat='density', height=4, aspect=1.5)
p.fig.set_dpi(100)
  • 没有p.fig.set_dpi(100)

在此处输入图像描述

  • p.fig.set_dpi(100)

在此处输入图像描述

sns.histplot

  • 可以使用figsize和/或调整轴级别图的大小dpi
# create figure and axes
fig, ax = plt.subplots(figsize=(6, 5), dpi=100)

# plot to the existing fig, by using ax=ax
p = sns.histplot(data=df, x='flipper_length_mm', stat='density', ax=ax)
  • 没有dpi=100

在此处输入图像描述

  • dpi=100

在此处输入图像描述

于 2021-11-21T20:30:39.710 回答
8

Paul H 和 J. Li 的最佳答案不适用于所有类型的 seaborn 人物。对于FacetGrid类型(例如sns.lmplot()),使用sizeandaspect参数。

Size改变高度和宽度,保持纵横比。

Aspect只改变宽度,保持高度不变。

您始终可以通过使用这两个参数来获得所需的尺寸。

信用:https ://stackoverflow.com/a/28765059/3901029

于 2018-09-25T00:42:30.007 回答
6

一些尝试过的方法:

import seaborn as sns
import matplotlib.pyplot as plt
ax, fig = plt.subplots(figsize=[15,7])
sns.boxplot(x="feature1", y="feature2",data=df) # where df would be your dataframe

或者

import seaborn as sns
import matplotlib.pyplot as plt
plt.figure(figsize=[15,7])
sns.boxplot(x="feature1", y="feature2",data=df) # where df would be your dataframe
于 2021-05-16T10:58:39.367 回答