我如何改变我的图像的大小,使其适合打印?
例如,我想使用A4纸,它的尺寸是11.7英寸乘8.27英寸。
我如何改变我的图像的大小,使其适合打印?
例如,我想使用A4纸,它的尺寸是11.7英寸乘8.27英寸。
当前回答
See How to change the image size for seaborn.objects for a solution with the new seaborn.objects interface from seaborn v0.12, which is not the same as seaborn axes-level or figure-level plots. Adjusting the size of the plot depends if the plot is a figure-level plot like seaborn.displot, or an axes-level plot like seaborn.histplot. This answer applies to any figure or axes level plots. See the the seaborn API reference seaborn is a high-level API for matplotlib, so seaborn works with matplotlib methods Tested in 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
图形级图的大小可以通过高度和/或纵横参数进行调整 此外,图的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
其他回答
您可以将上下文设置为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')
这也会起作用。
from matplotlib import pyplot as plt
import seaborn as sns
plt.figure(figsize=(15,16))
sns.countplot(data=yourdata, ...)
你需要提前创建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)
这可以使用:
plt.figure(figsize=(15,8))
sns.kdeplot(data,shade=True)
See How to change the image size for seaborn.objects for a solution with the new seaborn.objects interface from seaborn v0.12, which is not the same as seaborn axes-level or figure-level plots. Adjusting the size of the plot depends if the plot is a figure-level plot like seaborn.displot, or an axes-level plot like seaborn.histplot. This answer applies to any figure or axes level plots. See the the seaborn API reference seaborn is a high-level API for matplotlib, so seaborn works with matplotlib methods Tested in 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
图形级图的大小可以通过高度和/或纵横参数进行调整 此外,图的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