-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpand_coordinates_example.py
More file actions
282 lines (227 loc) · 8.65 KB
/
Copy pathexpand_coordinates_example.py
File metadata and controls
282 lines (227 loc) · 8.65 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#!/usr/bin/env python3
"""
Example script demonstrating the expand_subplot_coordinates function.
This script shows practical usage of the expand_subplot_coordinates function
for figure insertion and layout planning.
"""
import matplotlib.pyplot as plt
import numpy as np
from yplot.figure import SubplotLayout, calculate_subplot_coordinates
from yplot.layout_utils import expand_subplot_coordinates
from yplot.plotting import create_figure_with_layout
from yplot.style import publication_style_ax
def example_1_basic_expansion():
"""Example 1: Basic expansion of a single subplot coordinate."""
print("Example 1: Basic expansion of a single subplot coordinate")
# Original subplot coordinate
coord = (0.2, 0.2, 0.6, 0.6)
print(f"Original coordinate: {coord}")
# Expand it
expanded = expand_subplot_coordinates(
coord,
fig_size_inches=(10, 8),
margins={"left": 0.5, "right": 0.5, "top": 0.5, "bottom": 0.5},
spacing={"hspace": 0.3, "wspace": 0.3},
)
print(f"Expanded coordinate: {expanded}")
print(
f"Expansion factor: {expanded[2]/coord[2]:.2f}x width, {expanded[3]/coord[3]:.2f}x height"
)
print()
def example_2_figure_insertion():
"""Example 2: Practical figure insertion demonstration."""
print("Example 2: Figure insertion demonstration")
# Create a subplot layout using SubplotLayout
layout = SubplotLayout(fig_size_inches=(12, 8), rows=2, cols=3)
layout.set_uniform_row_height(2.0)
layout.set_uniform_col_width(2.5)
layout.set_uniform_wspace(0.4)
layout.set_uniform_hspace(0.4)
layout.margins = {"left": 0.5, "right": 0.5, "top": 0.5, "bottom": 0.5}
# Get coordinates
coords = calculate_subplot_coordinates(layout)
# Expand the first subplot for figure insertion
expanded_coord = expand_subplot_coordinates(
coords[0],
fig_size_inches=(12, 8),
margins={"left": 0.5, "right": 0.5, "top": 0.5, "bottom": 0.5},
spacing={"hspace": 0.4, "wspace": 0.4},
)
print(f"Original subplot coordinate: {coords[0]}")
print(f"Expanded coordinate for insertion: {expanded_coord}")
# Create a demonstration figure
fig = plt.figure(figsize=(12, 8))
# Add all original subplots
for i, coord in enumerate(coords):
ax = fig.add_axes(coord)
ax.plot([0, 1], [0, 1], "b-", linewidth=2)
ax.set_title(f"Subplot {i}", fontsize=10)
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
ax.grid(True, alpha=0.3)
publication_style_ax(ax)
# Add expanded area for figure insertion
expanded_ax = fig.add_axes(expanded_coord)
expanded_ax.set_facecolor("lightyellow")
expanded_ax.set_alpha(0.3)
expanded_ax.set_title(
"Expanded Area for Figure Insertion", fontsize=12, fontweight="bold"
)
expanded_ax.set_xticks([])
expanded_ax.set_yticks([])
for spine in expanded_ax.spines.values():
spine.set_visible(False)
# Add text explaining the use case
expanded_ax.text(
0.5,
0.5,
"This expanded area includes\nmargins and spacing\nfor figure insertion",
ha="center",
va="center",
fontsize=10,
bbox=dict(boxstyle="round,pad=0.3", facecolor="white", alpha=0.8),
)
fig.suptitle(
"Example 2: Figure Insertion Demonstration", fontsize=16, fontweight="bold"
)
# Save the figure
fig.savefig(
"docs/figures/example_02_figure_insertion.png", dpi=150, bbox_inches="tight"
)
plt.close(fig)
print("Saved demonstration figure: docs/figures/example_02_figure_insertion.png")
print()
def example_3_multiple_expansion():
"""Example 3: Expanding multiple subplot coordinates."""
print("Example 3: Expanding multiple subplot coordinates")
# Multiple subplot coordinates
coords = [
(0.1, 0.1, 0.3, 0.3), # Bottom left
(0.6, 0.1, 0.3, 0.3), # Bottom right
(0.1, 0.6, 0.3, 0.3), # Top left
(0.6, 0.6, 0.3, 0.3), # Top right
]
print("Original coordinates:")
for i, coord in enumerate(coords):
print(f" Subplot {i}: {coord}")
# Expand them
expanded_coords = expand_subplot_coordinates(
coords,
fig_size_inches=(12, 10),
margins={"left": 0.75, "right": 0.75, "top": 0.75, "bottom": 0.75},
spacing={"hspace": 0.5, "wspace": 0.5},
)
print("\nExpanded coordinates:")
for i, coord in enumerate(expanded_coords):
print(f" Subplot {i}: {coord}")
# Calculate expansion factors
print("\nExpansion factors:")
for i, (orig, exp) in enumerate(zip(coords, expanded_coords)):
width_factor = exp[2] / orig[2]
height_factor = exp[3] / orig[3]
print(f" Subplot {i}: {width_factor:.2f}x width, {height_factor:.2f}x height")
print()
def example_4_with_without_spacing():
"""Example 4: Comparison of with and without adjacent spacing."""
print("Example 4: Comparison of with and without adjacent spacing")
# Original coordinate
coord = (0.2, 0.2, 0.6, 0.6)
# Expand with adjacent spacing
expanded_with = expand_subplot_coordinates(
coord,
fig_size_inches=(10, 8),
margins={"left": 0.5, "right": 0.5, "top": 0.5, "bottom": 0.5},
spacing={"hspace": 0.3, "wspace": 0.3},
include_adjacent_spacing=True,
)
# Expand without adjacent spacing
expanded_without = expand_subplot_coordinates(
coord,
fig_size_inches=(10, 8),
margins={"left": 0.5, "right": 0.5, "top": 0.5, "bottom": 0.5},
spacing={"hspace": 0.3, "wspace": 0.3},
include_adjacent_spacing=False,
)
print(f"Original coordinate: {coord}")
print(f"With adjacent spacing: {expanded_with}")
print(f"Without adjacent spacing: {expanded_without}")
# Calculate differences
width_diff = expanded_with[2] - expanded_without[2]
height_diff = expanded_with[3] - expanded_without[3]
print(
f"Difference (with - without): {width_diff:.4f} width, {height_diff:.4f} height"
)
print()
def example_5_create_figure_with_layout():
"""Example 5: Using create_figure_with_layout for easy figure creation."""
print("Example 5: Using create_figure_with_layout")
# Create layout using SubplotLayout
layout = SubplotLayout(fig_size_inches=(10, 6), rows=2, cols=3)
layout.row_heights = [2.5, 2.0]
layout.col_widths = [2.5, 2.5, 2.5]
layout.set_uniform_wspace(0.3)
layout.set_uniform_hspace(0.2)
layout.margins = {"left": 0.5, "right": 0.5, "top": 0.5, "bottom": 0.5}
# Create figure and axes in one step
fig, axes = create_figure_with_layout(layout)
# Add some content to demonstrate
for i, ax in enumerate(axes):
x = np.linspace(0, 2 * np.pi, 100)
y = np.sin(x + i * np.pi / 6)
ax.plot(x, y, linewidth=2)
ax.set_title(f"Plot {i+1}", fontsize=12)
ax.grid(True, alpha=0.3)
publication_style_ax(ax)
fig.suptitle("Example 5: create_figure_with_layout", fontsize=16, fontweight="bold")
# Save the figure
fig.savefig(
"docs/figures/example_05_create_figure_with_layout.png",
dpi=150,
bbox_inches="tight",
)
plt.close(fig)
print(
"Saved demonstration figure: docs/figures/example_05_create_figure_with_layout.png"
)
print()
def example_6_per_row_spacing():
"""Example 6: Expansion with per-row spacing."""
print("Example 6: Expansion with per-row spacing")
# Multiple subplot coordinates
coords = [
(0.1, 0.1, 0.3, 0.3), # Bottom left
(0.6, 0.1, 0.3, 0.3), # Bottom right
(0.1, 0.6, 0.3, 0.3), # Top left
(0.6, 0.6, 0.3, 0.3), # Top right
]
# Expand with per-row spacing
expanded_coords = expand_subplot_coordinates(
coords,
fig_size_inches=(12, 10),
margins={"left": 0.5, "right": 0.5, "top": 0.5, "bottom": 0.5},
spacing={"hspace": [0.2, 0.4], "wspace": [0.3, 0.6]},
)
print("Original coordinates:")
for i, coord in enumerate(coords):
print(f" Subplot {i}: {coord}")
print("\nExpanded coordinates (per-row spacing):")
for i, coord in enumerate(expanded_coords):
print(f" Subplot {i}: {coord}")
print()
def run_all_examples():
"""Run all example functions."""
print("=" * 60)
print("Expand Subplot Coordinates - Examples")
print("=" * 60)
print()
example_1_basic_expansion()
example_2_figure_insertion()
example_3_multiple_expansion()
example_4_with_without_spacing()
example_5_create_figure_with_layout()
example_6_per_row_spacing()
print("=" * 60)
print("All examples completed successfully!")
print("=" * 60)
if __name__ == "__main__":
run_all_examples()