stu*_*ent 172
您还可以通过在seaborn 方法中rc
使用键将字典传递给参数来设置图形大小:'figure.figsize'
set
import seaborn as sns
sns.set(rc={'figure.figsize':(11.7,8.27)})
Run Code Online (Sandbox Code Playgroud)
其他替代可以是使用figure.figsize
的rcParams
设置的数字大小如下:
from matplotlib import rcParams
# figure size in inches
rcParams['figure.figsize'] = 11.7,8.27
Run Code Online (Sandbox Code Playgroud)
更多细节可以在matplotlib文档中找到
Pau*_*l H 151
您需要提前创建matplotlib图和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)
Run Code Online (Sandbox Code Playgroud)
Jia*_* Li 86
您可以设置上下文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')
Run Code Online (Sandbox Code Playgroud)
elz*_*elz 55
请注意,如果你正在试图通过一个"数字级别"的方法在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)
Run Code Online (Sandbox Code Playgroud)
有关图级方法不遵守轴规范这一事实的更多详细信息,请参阅https://github.com/mwaskom/seaborn/issues/488和使用matplotlib面向对象接口的seaborn绘图.
小智 30
首先导入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)
Run Code Online (Sandbox Code Playgroud)
Tre*_*ney 24
seaborn.objects
以获取使用来自 的新界面的解决方案seaborn v0.12
,这与seaborn轴级或图形级图不同。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')
Run Code Online (Sandbox Code Playgroud)
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)
Run Code Online (Sandbox Code Playgroud)
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)
Run Code Online (Sandbox Code Playgroud)
dpi=100
dpi=100
Jer*_*110 15
# 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()
Run Code Online (Sandbox Code Playgroud)
hea*_*ad7 14
对于我的情节(sns因子图),建议的答案不起作用。
因此我用
plt.gcf().set_size_inches(11.7, 8.27)
Run Code Online (Sandbox Code Playgroud)
紧随seaborn的情节之后(因此无需将斧头传递给seaborn或更改rc设置)。
Muf*_*hir 11
这也将起作用。
from matplotlib import pyplot as plt
import seaborn as sns
plt.figure(figsize=(15,16))
sns.countplot(data=yourdata, ...)
Run Code Online (Sandbox Code Playgroud)
rms*_*wrp 10
一些尝试过的方法:
import seaborn as sns
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=[15,7])
sns.boxplot(x="feature1", y="feature2",data=df, ax=ax) # where df would be your dataframe
Run Code Online (Sandbox Code Playgroud)
或者
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
Run Code Online (Sandbox Code Playgroud)
可以使用以下方法完成:
plt.figure(figsize=(15,8))
sns.kdeplot(data,shade=True)
Run Code Online (Sandbox Code Playgroud)
保罗·H(Paul H)和李·李(J. Li)给出的头等答案并不适合所有类型的海洋人物。对于FacetGrid
类型(例如sns.lmplot()
),请使用size
和aspect
参数。
Size
更改高度和宽度,同时保持宽高比。
Aspect
仅改变宽度,保持高度不变。
通过使用这两个参数,您始终可以获得所需的大小。
信用:https : //stackoverflow.com/a/28765059/3901029
小智 6
除了关于返回多图网格对象的“图形级别”方法的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)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
264589 次 |
最近记录: |