Skip to content

Commit 1231847

Browse files
committed
Python: improve legend_location
1 parent eba88e3 commit 1231847

File tree

7 files changed

+75
-80
lines changed

7 files changed

+75
-80
lines changed

Utilities/Python/FDS_verification_dataplot_inputs.csv

Lines changed: 50 additions & 50 deletions
Large diffs are not rendered by default.

Utilities/Python/fdsplotlib.py

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,9 @@ def read_csv_cached(path, **kwargs):
424424
y_min=pp.Min_Ind if flip_axis else pp.Min_Dep,
425425
y_max=pp.Max_Ind if flip_axis else pp.Max_Dep,
426426
legend_location=matlab_legend_to_matplotlib(pp.Key_Position),
427+
legend_expand=pp.Paper_Width_Factor,
427428
plot_type=plot_type,
429+
plot_title=pp.Plot_Title,
428430
)
429431

430432
# --- Save measured (experimental) ---
@@ -530,15 +532,6 @@ def read_csv_cached(path, **kwargs):
530532
figure_handle=f, # keep same figure
531533
data_label=d2_key_labels[i],
532534
line_style=d2_styles[i],
533-
x_label=pp.Dep_Title if flip_axis else pp.Ind_Title,
534-
y_label=pp.Ind_Title if flip_axis else pp.Dep_Title,
535-
x_min=pp.Min_Dep if flip_axis else pp.Min_Ind,
536-
x_max=pp.Max_Dep if flip_axis else pp.Max_Ind,
537-
y_min=pp.Min_Ind if flip_axis else pp.Min_Dep,
538-
y_max=pp.Max_Ind if flip_axis else pp.Max_Dep,
539-
legend_location=matlab_legend_to_matplotlib(pp.Key_Position),
540-
plot_type=plot_type,
541-
plot_title=pp.Plot_Title,
542535
)
543536

544537
# --- Interpolated, metric-aware model logic ---
@@ -800,7 +793,7 @@ def plot_to_fig(x_data,y_data,**kwargs):
800793
ax = fig.add_axes([left, bottom, ax_w, ax_h])
801794

802795
# widen paper if legend is outside, keeping axes fixed in physical size
803-
if kwargs.get('legend_location') == 'outside':
796+
if (kwargs.get('legend_location') == 'outside') and (not using_existing_figure):
804797
legend_expand = kwargs.get('legend_expand', 1.25)
805798
old_size = fig.get_size_inches()
806799

@@ -1339,14 +1332,13 @@ def get_plot_style(style="fds"):
13391332

13401333
def matlab_legend_to_matplotlib(position):
13411334
"""
1342-
Convert a MATLAB legend position string to the corresponding matplotlib location string.
1335+
Convert a MATLAB legend position string to a matplotlib-compatible location.
1336+
"""
1337+
if not isinstance(position, str):
1338+
return 'best'
13431339

1344-
Parameters:
1345-
position (str): MATLAB-style legend position (e.g., 'northeast', 'southwestoutside')
1340+
pos = position.strip().lower()
13461341

1347-
Returns:
1348-
str: Matplotlib-compatible legend location (e.g., 'upper right')
1349-
"""
13501342
mapping = {
13511343
'north': 'upper center',
13521344
'south': 'lower center',
@@ -1356,17 +1348,15 @@ def matlab_legend_to_matplotlib(position):
13561348
'southeast': 'lower right',
13571349
'southwest': 'lower left',
13581350
'northwest': 'upper left',
1359-
'northeastoutside': 'center left', # rough equivalent
1360-
'northwestoutside': 'center right',
1361-
'southeastoutside': 'center left',
1362-
'southwestoutside': 'center right',
1351+
1352+
# outside positions (MATLAB → Matplotlib approximations)
1353+
'eastoutside': 'outside',
1354+
13631355
'best': 'best'
13641356
}
13651357

1366-
if not isinstance(position, str):
1367-
return 'best'
1358+
return mapping.get(pos, 'best')
13681359

1369-
return mapping.get(position.strip().lower(), 'best')
13701360

13711361

13721362
def define_plot_parameters(D, irow, lightweight=False):

Utilities/Python/scripts/fluid_part.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -469,13 +469,14 @@ def read_prt5(filename, precision, *varargin):
469469
STIME, XP, YP, ZP, QP = read_prt5(ddir + chid[i] + '_1.prt5', 'real*4')
470470

471471
if j[i] == 1:
472-
fdsplotlib.plot_to_fig(x_data=XP[-1, :], y_data=QP[-1, :, 0, 0]/QP[-1, :, 0, 1], figure_handle=fig, marker_style='bo', data_label='FDS part')
472+
fdsplotlib.plot_to_fig(x_data=XP[-1, :], y_data=QP[-1, :, 0, 0]/QP[-1, :, 0, 1], figure_handle=fig, marker_style='bo')
473473
v = np.abs(c_d * a_p * 0.5*rho_g*(XP[-1, :]**2) - QP[-1, :, 0, 0]/QP[-1, :, 0, 1])
474474
elif j[i] == 2:
475-
fdsplotlib.plot_to_fig(x_data=YP[-1, :], y_data=QP[-1, :, 0, 0]/QP[-1, :, 0, 1], figure_handle=fig, marker_style='ro', data_label='FDS part')
475+
fdsplotlib.plot_to_fig(x_data=YP[-1, :], y_data=QP[-1, :, 0, 0]/QP[-1, :, 0, 1], figure_handle=fig, marker_style='ro')
476476
v = np.abs(c_d * a_p * 0.5*rho_g*(YP[-1, :]**2) - QP[-1, :, 0, 0]/QP[-1, :, 0, 1])
477477
elif j[i] == 3:
478-
fdsplotlib.plot_to_fig(x_data=ZP[-1, :], y_data=QP[-1, :, 0, 0]/QP[-1, :, 0, 1], figure_handle=fig, marker_style='go', data_label='FDS part')
478+
fds_key_label = 'FDS part' if i==2 else None
479+
fdsplotlib.plot_to_fig(x_data=ZP[-1, :], y_data=QP[-1, :, 0, 0]/QP[-1, :, 0, 1], figure_handle=fig, marker_style='go', data_label=fds_key_label)
479480
v = np.abs(c_d * a_p * 0.5*rho_g*(ZP[-1, :]**2) - QP[-1, :, 0, 0]/QP[-1, :, 0, 1])
480481

481482
err = np.linalg.norm(v)/len(v)

Utilities/Python/scripts/level_set_ellipse.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ def virtual_wind(phi_s_x: float, phi_s_y: float) -> tuple[float, float]:
104104
plot_title=f'Level set spread rate (LS_ellipse_*ms_{slope:02d}deg)',
105105
x_label='x (m)',
106106
y_label='y (m)',
107-
data_label=f'FDS {U} m/s'
107+
data_label=f'FDS {U} m/s',
108+
legend_location='lower right',
108109
)
109110
else:
110111
fdsplotlib.plot_to_fig(x_data=x_pts,

Utilities/Python/scripts/radiating_polygon.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@
5858
plot_title = 'Radiative Flux from a Hot Square Plate',
5959
data_label='Exact',
6060
y_label='Radiative Heat Flux (kW/m$^2$)',
61-
x_label='Distance from Plate (m)')
61+
x_label='Distance from Plate (m)',
62+
legend_location='lower left')
6263

6364
fdsplotlib.plot_to_fig(x_data=1-flux_20[0], y_data=flux_20[1], marker_style='r-',
6465
figure_handle=fig,

Utilities/Python/scripts/tree_shapes.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@
5858
revision_label=version_string,
5959
plot_title='Initialized bulk density (tree_shapes)',
6060
x_label=r'Input volume (m$^3$)',
61-
y_label='Tree crown mass (kg)'
61+
y_label='Tree crown mass (kg)',
62+
legend_location='lower right',
6263
)
6364

6465
# Plot results

Utilities/Python/scripts/turb_model.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@ def energy_decay(chid, N):
154154
x_min=0, x_max=0.7, y_min=0, y_max=0.07,
155155
revision_label=version_string,
156156
x_label='Time (s)',
157-
y_label='Kinetic Energy (m$^2$/s$^2$)')
157+
y_label='Kinetic Energy (m$^2$/s$^2$)',
158+
legend_location='upper right')
158159

159160
if chid == 'csmag_32':
160161
M = pd.read_csv(outdir + 'mu0_32_devc.csv', skiprows=2, header=None).values

0 commit comments

Comments
 (0)