【发布时间】:2020-05-24 22:25:30
【问题描述】:
如何更改图像的大小以使其适合打印?
例如,我想使用 A4 纸,横向尺寸为 11.7 英寸 x 8.27 英寸。
【问题讨论】:
标签: python matplotlib seaborn figsize
如何更改图像的大小以使其适合打印?
例如,我想使用 A4 纸,横向尺寸为 11.7 英寸 x 8.27 英寸。
【问题讨论】:
标签: python matplotlib seaborn figsize
您还可以通过在 seaborn set 方法中使用键 'figure.figsize' 将字典传递给 rc 参数来设置图形大小:
import seaborn as sns
sns.set(rc={'figure.figsize':(11.7,8.27)})
其他替代方法可能是使用rcParams 的figure.figsize 来设置图形大小如下:
from matplotlib import rcParams
# figure size in inches
rcParams['figure.figsize'] = 11.7,8.27
更多详情请见matplotlib documentation
【讨论】:
.set_style() 之前致电.set()
fig, ax = pyplot.subplots(figsize=(20, 2)); a = sns.lineplot(ax=ax, x=..., y=...) 会按预期工作。当需要使用“技巧”设置这些参数时,我总是感到惊讶,因为这些参数在 seaborn 中应该很简单,因为它经常使用。
您需要提前创建 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)
【讨论】:
sns.lmplot()
height 和aspect 参数设置,如此处所述stackoverflow.com/a/51602446/2412831
请注意,如果您尝试传递到 seaborn 中的“图形级别”方法(例如 lmplot、catplot / factorplot、jointplot),您可以并且应该使用 @ 在参数中指定它987654328@和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 和Plotting with seaborn using the matplotlib object-oriented interface,了解有关图形级别方法不遵守轴规范的更多详细信息。
【讨论】:
您可以将上下文设置为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')
【讨论】:
sns.lmplot()
先导入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)
【讨论】:
这可以使用:
plt.figure(figsize=(15,8))
sns.kdeplot(data,shade=True)
【讨论】:
import matplotlib.pyplot as plt 来使用这个对图形的简单调用,那就太好了。我想知道他们为什么不直接用sns.set_figure_size() 公开它。
除了 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)
【讨论】:
set_figwidth 和 set_figheight 适用于网格对象。 >>> import seaborn >>> import matplotlib.pyplot as pyplot >>> Tips = seaborn.load_dataset("tips") >>> g = seaborn.FacetGrid(tips, col="time", row="smoker") >>> g = g.map(pyplot.hist, "total_bill") >>> g.fig.set_figwidth(10) >>> g.fig.set_figheight(10)
0.11.1 - 此页面上的其他解决方案无效
这也应该有效。
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 设置)。
【讨论】:
python g = sns.FacetGrid(df.set_index('category'), col="id") pyplot.gcf().set_size_inches(11.7, 8.27) g.map(lambda data, color: data.plot.barh(color=color), "count")
sns.FacetGrid 会根据计算值(由height 和aspect 设置)设置图形大小,并在seaborn 绘图后直接更改图形大小。更改图形大小后可能会发生其他情节的微调
Paul H 和 J. Li 的最佳答案不适用于所有类型的 seaborn 人物。对于FacetGrid 类型(例如sns.lmplot()),使用size 和aspect 参数。
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
【讨论】:
seaborn.displot 这样的图形级绘图,还是像seaborn.histplot 这样的轴级绘图。 此答案适用于任何图形或坐标轴水平图。
seaborn 是 matplotlib 的高级 API,因此 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.displotheight 和/或aspect 参数调整图形级别图的大小fig对象并使用.set_dpi()来设置图的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.histplotfigsize 和/或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
【讨论】: