-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07_spec_diff_and_migrate.py
More file actions
71 lines (56 loc) · 2.55 KB
/
07_spec_diff_and_migrate.py
File metadata and controls
71 lines (56 loc) · 2.55 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
"""
Spec comparison and figure migration between journals.
Steps:
1. Compare two journal specs with plotstyle.diff() to see what changes.
2. Create a figure styled for the source journal (Nature).
3. Migrate it to a new journal with plotstyle.migrate() — resizes the figure,
rescales text, and applies the target journal's rcParams in-place.
Output:
output/before_migration_nature.pdf
output/after_migration_science.pdf
"""
import sys
from pathlib import Path
import matplotlib.pyplot as plt
import numpy as np
import plotstyle
# Arrow characters in the diff table require UTF-8 output.
sys.stdout.reconfigure(encoding="utf-8")
OUTPUT_DIR = Path(__file__).parent / "output"
OUTPUT_DIR.mkdir(exist_ok=True)
# ==============================================================================
# 1. Compare two journal specs
# ==============================================================================
result = plotstyle.diff("nature", "science")
if result:
print("Differences between Nature and Science:")
print(result)
print(f"\nTotal differing fields: {len(result)}")
else:
print("Nature and Science specs are identical.")
# Each SpecDifference has .label, .value_a, .value_b for programmatic access
print("\nDetailed differences:")
for d in result.differences:
print(f" {d.label}: {d.value_a} -> {d.value_b}")
# ==============================================================================
# 2. Create a figure styled for Nature
# ==============================================================================
with plotstyle.use("nature") as style:
fig, ax = style.figure(columns=1)
x = np.linspace(0, 10, 100)
ax.plot(x, np.sin(x), label="Data")
ax.fill_between(x, np.sin(x) - 0.2, np.sin(x) + 0.2, alpha=0.3)
ax.set_xlabel("Wavelength (nm)")
ax.set_ylabel("Intensity (a.u.)")
ax.legend()
# Save the pre-migration version for comparison
style.savefig(fig, OUTPUT_DIR / "before_migration_nature.pdf")
# ==============================================================================
# 3. Migrate the figure from Nature to Science
# ==============================================================================
# migrate() mutates the figure in-place: resizes canvas, rescales text, applies rcParams.
# Any significant changes (font family switch, DPI increase, etc.) are printed as warnings.
plotstyle.migrate(fig, from_journal="nature", to_journal="science")
# Save the post-migration figure with Science-compliant settings
plotstyle.savefig(fig, OUTPUT_DIR / "after_migration_science.pdf", journal="science")
plt.close(fig)