Skip to content

Latest commit

 

History

History
125 lines (90 loc) · 2.92 KB

File metadata and controls

125 lines (90 loc) · 2.92 KB

fig.py 使用指南

概述

fig.py 是一个用于生成专业学术图表的Python模块,提供了预设的图形配置,适用于期刊论文、会议幻灯片等不同发布场景。

主要特性

  • 🎯 预设配置:提供6种预设配置,适应不同发布需求
  • 📏 精确布局:支持多子图精确定位和间距控制
  • 🔤 LaTeX支持:内置LaTeX渲染,生成高质量数学公式
  • 📐 灵活自定义:可覆盖任意配置参数

快速开始

1. 最简单的使用

from fig import PlotConfig
import matplotlib.pyplot as plt
import numpy as np

# 创建配置对象
config = PlotConfig()

# 获取图形和坐标轴
fig, ax = config.get_simple()

# 绘图
x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)
ax.plot(x, y, label=r'$\sin(x)$')
ax.set_xlabel(r'$x$')
ax.set_ylabel(r'$y$')
ax.legend()

# 保存
plt.savefig('my_plot.pdf', bbox_inches='tight')
plt.close(fig)

2. 选择不同配置

# 论文单栏(默认)
config = PlotConfig('manuscript_single')

# 论文双栏
config = PlotConfig('manuscript_double')

# 幻灯片格式(字体更大)
config = PlotConfig('slides_single')

# PCI期刊格式
config = PlotConfig('PCI_single')

3. 自定义参数

config = PlotConfig(
    config_name='manuscript_single',
    ftsize=14,           # 字体大小
    plot_width=10.0,     # 图形宽度(cm)
    subplot_ratio=0.8    # 子图宽高比
)

4. 多子图布局

# 创建2x2布局
config = PlotConfig('manuscript_double', nrow=2, ncol=2)
fig = config.get_fig()

# 获取各个子图
ax1 = config.get_axes(fig, i=0, j=0)  # 左上
ax2 = config.get_axes(fig, i=0, j=1)  # 右上
ax3 = config.get_axes(fig, i=1, j=0)  # 左下
ax4 = config.get_axes(fig, i=1, j=1)  # 右下

# 分别绘图...

配置说明

配置名称 用途 图宽(cm) 字体大小
manuscript_single 期刊论文单栏 9.0 11
manuscript_double 期刊论文双栏 19.0 11
slides_single 幻灯片单图 9.0 16
slides_double 幻灯片双图 19.0 16
PCI_single PCI期刊单图 6.7 9
PCI_double PCI期刊双图 13.7 9

主要方法

PlotConfig类

  • __init__(config_name, nrow, ncol, **kwargs): 初始化配置
  • get_fig(**kwargs): 创建图形对象
  • get_axes(fig, i, j, **kwargs): 获取指定位置的坐标轴
  • get_simple(): 快速获取单图的fig和ax

工具函数

  • close(fig): 关闭图形

运行示例

要查看完整的使用示例,请运行:

python fig_examples.py

这将生成5个示例PDF文件,展示不同的使用场景。

注意事项

  1. 确保安装了matplotlib和LaTeX环境
  2. 所有尺寸单位为厘米(cm)
  3. 使用LaTeX语法编写数学公式,如 r'$\sin(x)$'
  4. 建议保存为PDF格式以获得最佳质量
  5. 记得在绘图完成后调用close(fig)释放内存