You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Seaborn设置figsize为LaTeX的\textwidth时图形远小于预期的问题求助

Seaborn设置figsize为LaTeX的\textwidth时图形远小于预期的问题求助

我现在在调整Seaborn绘图的图形和字体大小,目的是让导出的图能完美适配LaTeX文档用于发表,但遇到了一个头疼的问题:

我的LaTeX文档里\textwidth设置为7.00925英寸,我把matplotlib的figsize设成(textwidth, 3),但导出的图放进LaTeX后看起来特别小还很挤(对应第一张生成的图);但如果把figsize直接翻倍成(textwidth*2, 3*2),出来的效果就完全符合预期了。

我不想用翻倍的方式解决,因为这样字体大小会和LaTeX正文不一致,还要额外做转换调整,太繁琐了。我查过资料,matplotlib的figsize单位应该是英寸,但现在的表现感觉像是用了厘米当单位?有没有大佬能帮我分析下这是怎么回事?提前谢谢大家了!

相关参数与生成说明

  • 用fig_size=(textwidth, 3)生成的图:图形过小且拥挤
  • 用fig_size=(textwidth*2, 3*2)生成的图:效果符合预期
  • 核心参数值:
    textwidth = 7.00925
    y_vars = ['test_1', 'test_2', 'test_3']
    titles = ['Test 1', 'Test 2', 'Test 3']
    

绘图代码

import matplotlib.pyplot as plt
import seaborn as sns

fig, axes = plt.subplots(1, 3, figsize=(textwidth, 3), sharey=True)
for i, (y, title) in enumerate(zip(y_vars, titles)):
    ax = axes[i]
    # 绘制散点图
    sns.stripplot(
        x='diagnosis_grouped', y=y, data=plot_input, ax=ax,
        palette='viridis', alpha=0.7, jitter=True
    )
    # 绘制箱线图
    sns.boxplot(
        x='group', y=y, data=plot_input, ax=ax,
        showfliers=False, width=0.3,
        boxprops=dict(facecolor='none', edgecolor='black'),
        medianprops=dict(color='black'),
        whiskerprops=dict(color='black'),
        capprops=dict(color='black')
    )
    ax.set_title(title, fontsize=font_size)
    ax.set_xlabel('Group', fontsize=font_size)
    if i == 0:
        ax.set_ylabel('Score', fontsize=font_size)
    else:
        ax.set_ylabel('')
    ax.tick_params(axis='x', rotation=20, labelsize=font_size)
    ax.tick_params(axis='y', labelsize=font_size)

plt.tight_layout(rect=[0, 0, 1, 0.95])
plt.savefig('/path/figure_1.eps', dpi=800)

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.07 12:24:32