pit*_*las 1 python matplotlib seaborn
sns.boxplot(data=df, width=0.5)
plt.title(f'Distribution of scores for initial and resubmission\
\nonly among students who resubmitted at all.\
\n(n = {df.shape[0]})')
Run Code Online (Sandbox Code Playgroud)
我想使用更大的字体,并在顶部的白边留出更多的空间,这样标题就不会被塞进去。令人惊讶的是,尽管进行了一些认真的谷歌搜索,但我完全无法找到该选项!
您遇到的基本问题是多行标题太高,并且呈现为“离页”。
几个选项供您选择:
最省力的解决方案可能是使用tight_layout(). plt.tight_layout()操纵子图的位置和间距,使标签、刻度和标题更适合。
如果这还不够,还要看看plt.subplots_adjust()哪个可以让您控制一个或多个子图周围使用多少空格;您一次只能修改一个方面,而其他所有设置都保持不变。在您的情况下,您可以使用plt.subplots_adjust(top=0.8).
如果您正在生成用于出版或类似用途的最终数字,您可能打算进行大量调整以完善它。在这种情况下,您可以使用add_axes(参见此示例/sf/answers/1223559221/)精确控制(子)绘图位置。
这是一个示例,标题为 6 行以示强调。左侧面板显示默认值 - 一半的标题被剪掉。右侧面板的所有尺寸都相同,但顶部除外;中间已经自动去除了四面八方的空白。
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
data = 55 + 5* np.random.randn(1000,) # some data
vlongtitle = "\n".join(["long title"]*6) # a 6-line title
# using tight_layout, all the margins are reduced
plt.figure()
sns.boxplot(data, width=0.5)
plt.title(vlongtitle)
plt.tight_layout()
# 2nd option, just edit one aspect.
plt.figure()
sns.boxplot(data, width=0.5)
plt.title(vlongtitle)
plt.subplots_adjust(top=0.72)
Run Code Online (Sandbox Code Playgroud)