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.
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.
您还可以通过使用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 文档中找到
您需要提前创建 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)
请注意,如果您尝试传递给 seaborn 中的“图形级别”方法(例如/ lmplot
、 ) ,您可以并且应该在使用的参数中指定它and 。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)
您可以将上下文设置为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')
这可以使用以下方法完成:
plt.figure(figsize=(15,8))
sns.kdeplot(data,shade=True)
除了关于返回多图网格对象的“图形级别”方法的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)
这也应该有效。
from matplotlib import pyplot as plt
import seaborn as sns
plt.figure(figsize=(15,16))
sns.countplot(data=yourdata, ...)
对于我的情节(sns factorplot),建议的答案效果不佳。
因此我使用
plt.gcf().set_size_inches(11.7, 8.27)
就在与 seaborn 的情节之后(因此无需将斧头传递给 seaborn 或更改 rc 设置)。
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
Paul H 和 J. Li 的最佳答案不适用于所有类型的 seaborn 人物。对于FacetGrid
类型(例如sns.lmplot()
),使用size
andaspect
参数。
Size
改变高度和宽度,保持纵横比。
Aspect
只改变宽度,保持高度不变。
您始终可以通过使用这两个参数来获得所需的尺寸。
一些尝试过的方法:
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