forked from luzyPer/pyutils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfig_examples.py
More file actions
246 lines (196 loc) · 6.94 KB
/
fig_examples.py
File metadata and controls
246 lines (196 loc) · 6.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
"""
fig.py 使用示例
================
本文件展示了如何使用 fig.py 中的 PlotConfig 类进行自定义作图。
fig.py 提供了预设的图形配置,适用于不同的发布场景(论文、幻灯片等)。
作者:GitHub Copilot
日期:2025年10月12日
"""
import numpy as np
import matplotlib.pyplot as plt
from fig import PlotConfig, close
def example_1_simple_plot():
"""
示例1:最简单的使用方法 - 单个子图
使用默认的 manuscript_single 配置
"""
print("示例1:简单单图绘制")
# 创建PlotConfig对象,使用默认的manuscript_single配置
config = PlotConfig()
# 获取图形和坐标轴
fig, ax = config.get_simple()
# 生成示例数据
x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)
# 绘图
ax.plot(x, y, 'b-', linewidth=2, label=r'$y = \sin(x)$')
ax.set_xlabel(r'$x$')
ax.set_ylabel(r'$y$')
ax.set_title('简单正弦函数')
ax.legend()
ax.grid(True, alpha=0.3)
# 保存图片
plt.savefig('example_1_simple.pdf', bbox_inches='tight')
print("图片已保存为 example_1_simple.pdf")
# 关闭图形
close(fig)
def example_2_custom_config():
"""
示例2:使用不同的预设配置
演示如何选择不同的配置模板
"""
print("\n示例2:使用幻灯片配置")
# 使用幻灯片单图配置(字体更大,适合演示)
config = PlotConfig(config_name='slides_single')
fig, ax = config.get_simple()
# 生成示例数据
x = np.linspace(0, 10, 50)
y1 = np.exp(-x/5) * np.cos(x)
y2 = np.exp(-x/5) * np.sin(x)
# 绘制多条曲线
ax.plot(x, y1, 'r-', linewidth=3, label=r'$e^{-x/5}\cos(x)$')
ax.plot(x, y2, 'b--', linewidth=3, label=r'$e^{-x/5}\sin(x)$')
ax.set_xlabel(r'$x$')
ax.set_ylabel(r'$y$')
ax.set_title('阻尼振荡函数')
ax.legend()
ax.grid(True, alpha=0.3)
plt.savefig('example_2_slides.pdf', bbox_inches='tight')
print("图片已保存为 example_2_slides.pdf")
close(fig)
def example_3_custom_parameters():
"""
示例3:自定义参数
演示如何在创建PlotConfig时覆盖默认参数
"""
print("\n示例3:自定义参数配置")
# 自定义配置:修改字体大小和图形宽度
config = PlotConfig(
config_name='manuscript_single',
ftsize=14, # 增大字体
plot_width=12.0, # 增加图形宽度
subplot_ratio=0.7 # 改变宽高比
)
fig, ax = config.get_simple()
# 创建散点图示例
np.random.seed(42)
x = np.random.randn(100)
y = 2 * x + np.random.randn(100) * 0.5
ax.scatter(x, y, alpha=0.6, c='red', s=50)
# 拟合直线
coeffs = np.polyfit(x, y, 1)
x_fit = np.linspace(x.min(), x.max(), 100)
y_fit = coeffs[0] * x_fit + coeffs[1]
ax.plot(x_fit, y_fit, 'k--', linewidth=2,
label=f'拟合直线: $y = {coeffs[0]:.2f}x + {coeffs[1]:.2f}$')
ax.set_xlabel(r'$x$')
ax.set_ylabel(r'$y$')
ax.set_title('散点图与线性拟合')
ax.legend()
ax.grid(True, alpha=0.3)
plt.savefig('example_3_custom.pdf', bbox_inches='tight')
print("图片已保存为 example_3_custom.pdf")
close(fig)
def example_4_multiple_subplots():
"""
示例4:多子图布局
演示如何创建多个子图
"""
print("\n示例4:多子图布局 (2x2)")
# 创建2x2的子图布局
config = PlotConfig(config_name='manuscript_double', nrow=2, ncol=2)
fig = config.get_fig()
# 生成示例数据
x = np.linspace(0, 2*np.pi, 100)
# 子图1: 正弦函数
ax1 = config.get_axes(fig, i=0, j=0)
ax1.plot(x, np.sin(x), 'b-', linewidth=2)
ax1.set_title(r'$\sin(x)$')
ax1.grid(True, alpha=0.3)
# 子图2: 余弦函数
ax2 = config.get_axes(fig, i=0, j=1)
ax2.plot(x, np.cos(x), 'r-', linewidth=2)
ax2.set_title(r'$\cos(x)$')
ax2.grid(True, alpha=0.3)
# 子图3: 正切函数
ax3 = config.get_axes(fig, i=1, j=0)
y_tan = np.tan(x)
y_tan[np.abs(y_tan) > 10] = np.nan # 避免无穷大值
ax3.plot(x, y_tan, 'g-', linewidth=2)
ax3.set_title(r'$\tan(x)$')
ax3.set_ylim(-5, 5)
ax3.grid(True, alpha=0.3)
# 子图4: 指数函数
ax4 = config.get_axes(fig, i=1, j=1)
ax4.plot(x, np.exp(-x/2), 'm-', linewidth=2)
ax4.set_title(r'$e^{-x/2}$')
ax4.grid(True, alpha=0.3)
# 添加总标题
fig.suptitle('多种数学函数', fontsize=config.ftsize+2, y=0.95)
plt.savefig('example_4_multiple.pdf', bbox_inches='tight')
print("图片已保存为 example_4_multiple.pdf")
close(fig)
def example_5_pci_publication():
"""
示例5:期刊发表格式
演示PCI期刊的图形格式
"""
print("\n示例5:PCI期刊发表格式")
# 使用PCI期刊配置
config = PlotConfig(config_name='PCI_single')
fig, ax = config.get_simple()
# 创建热力学数据示例
T = np.linspace(300, 2000, 100) # 温度范围 300-2000K
# 假设的热容数据
Cp_N2 = 29.1 + 0.0014*T - 5.7e-7*T**2 + 1.1e-10*T**3
Cp_O2 = 29.4 + 0.0036*T - 1.9e-6*T**2 + 3.6e-10*T**3
Cp_CO2 = 22.3 + 0.06*T - 4.1e-5*T**2 + 1.3e-8*T**3
ax.plot(T, Cp_N2, 'b-', linewidth=1.5, label=r'N$_2$')
ax.plot(T, Cp_O2, 'r--', linewidth=1.5, label=r'O$_2$')
ax.plot(T, Cp_CO2, 'g-.', linewidth=1.5, label=r'CO$_2$')
ax.set_xlabel('Temperature (K)')
ax.set_ylabel(r'$C_p$ (J mol$^{-1}$ K$^{-1}$)')
ax.set_title('Heat Capacity vs Temperature')
ax.legend()
ax.grid(True, alpha=0.3)
plt.savefig('example_5_PCI.pdf', bbox_inches='tight')
print("图片已保存为 example_5_PCI.pdf")
close(fig)
def show_available_configs():
"""
显示所有可用的配置
"""
print("\n可用的预设配置:")
print("=" * 50)
configs = [
('manuscript_single', '单栏期刊论文格式'),
('manuscript_double', '双栏期刊论文格式'),
('slides_single', '单图幻灯片格式'),
('slides_double', '双图幻灯片格式'),
('PCI_single', 'PCI期刊单图格式'),
('PCI_double', 'PCI期刊双图格式')
]
for config_name, description in configs:
print(f"'{config_name}': {description}")
def main():
"""
主函数:运行所有示例
"""
print("fig.py 使用示例演示")
print("=" * 50)
# 显示可用配置
show_available_configs()
# 运行所有示例
example_1_simple_plot()
example_2_custom_config()
example_3_custom_parameters()
example_4_multiple_subplots()
example_5_pci_publication()
print("\n所有示例已完成!生成的PDF文件:")
print("- example_1_simple.pdf")
print("- example_2_slides.pdf")
print("- example_3_custom.pdf")
print("- example_4_multiple.pdf")
print("- example_5_PCI.pdf")
if __name__ == "__main__":
main()