Skip to content

Commit 979707b

Browse files
committed
Python: McCaffrey updates; better handling of ticks; fix revision string
1 parent ab33dc4 commit 979707b

File tree

2 files changed

+89
-80
lines changed

2 files changed

+89
-80
lines changed

Utilities/Python/fdsplotlib.py

Lines changed: 84 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -528,8 +528,8 @@ def read_csv_cached(path, **kwargs):
528528
f = plot_to_fig(
529529
x_data=y_i if flip_axis else x_i,
530530
y_data=x_i if flip_axis else y_i,
531-
revision_label=version_string,
532-
figure_handle=f, # keep same figure
531+
revision_label=version_string if dtest else None,
532+
figure_handle=f,
533533
data_label=d2_key_labels[i],
534534
line_style=d2_styles[i],
535535
)
@@ -857,14 +857,6 @@ def plot_to_fig(x_data,y_data,**kwargs):
857857
if linestyle == '-.': dashes = kwargs.get('line_dashes',(6, 3, 1, 3))
858858
if linestyle == ':': dashes = kwargs.get('line_dashes',(1, 3))
859859

860-
# This set is what we were using in Matlab
861-
# if linestyle == '': dashes = (None, None); linewidth = 0;
862-
# if linestyle == '-': dashes = (None, None)
863-
# if linestyle == '--': dashes = kwargs.get('line_dashes',(10, 6.2))
864-
# if linestyle == '-.': dashes = kwargs.get('line_dashes',(12, 7.4, 3, 7.4))
865-
# if linestyle == ':': dashes = kwargs.get('line_dashes',(1, 3))
866-
867-
868860
data_label = kwargs.get('data_label',None)
869861

870862
# trap any data_labels set to blank (old matlab convention)
@@ -1014,6 +1006,88 @@ def plot_to_fig(x_data,y_data,**kwargs):
10141006
fontsize=ax._legend_fontsize,
10151007
frameon=False)
10161008

1009+
# plot title
1010+
if kwargs.get('plot_title'):
1011+
if kwargs.get('title_fontsize'):
1012+
title_fontsize=kwargs.get('title_fontsize')
1013+
else:
1014+
title_fontsize=default_title_fontsize
1015+
1016+
plt.text(0.05, 0.95, kwargs.get('plot_title'),
1017+
transform=plt.gca().transAxes,
1018+
fontsize=title_fontsize,
1019+
verticalalignment='top',
1020+
horizontalalignment='left')
1021+
1022+
# set axes and tick properties
1023+
xmin=kwargs.get('x_min')
1024+
xmax=kwargs.get('x_max')
1025+
ymin=kwargs.get('y_min')
1026+
ymax=kwargs.get('y_max')
1027+
1028+
ax.set_xlim(xmin,xmax)
1029+
ax.set_ylim(ymin,ymax)
1030+
1031+
# ------------------------------------------------------------
1032+
# TICK HANDLING (clean, deterministic)
1033+
# ------------------------------------------------------------
1034+
1035+
scale_x = ax.get_xscale()
1036+
scale_y = ax.get_yscale()
1037+
1038+
# -------------------------------
1039+
# X-axis ticks
1040+
# -------------------------------
1041+
if xticks is not None:
1042+
# USER EXPLICIT OVERRIDE
1043+
ax.set_xticks(xticks)
1044+
ax.xaxis.set_major_formatter(ticker.FormatStrFormatter('%g'))
1045+
1046+
elif xnumticks is not None:
1047+
# USER requests a number of ticks
1048+
if scale_x == "log":
1049+
ax.set_xticks(np.logspace(np.log10(xmin), np.log10(xmax), xnumticks))
1050+
else:
1051+
ax.set_xticks(np.linspace(xmin, xmax, xnumticks))
1052+
1053+
else:
1054+
# DEFAULT behavior
1055+
if scale_x == "log":
1056+
ax.xaxis.set_major_locator(ticker.LogLocator(base=10))
1057+
ax.xaxis.set_major_formatter(ticker.LogFormatterSciNotation(base=10))
1058+
else:
1059+
ax.xaxis.set_major_locator(ticker.MaxNLocator(nbins=detault_nticks, min_n_ticks=4))
1060+
sf = ticker.ScalarFormatter(useMathText=True)
1061+
sf.set_powerlimits((-3, 4))
1062+
ax.xaxis.set_major_formatter(sf)
1063+
1064+
# -------------------------------
1065+
# Y-axis ticks
1066+
# -------------------------------
1067+
if yticks is not None:
1068+
# USER EXPLICIT OVERRIDE
1069+
ax.set_yticks(yticks)
1070+
ax.yaxis.set_major_formatter(ticker.FormatStrFormatter('%g'))
1071+
1072+
elif ynumticks is not None:
1073+
# USER requests a number of ticks
1074+
if scale_y == "log":
1075+
ax.set_yticks(np.logspace(np.log10(ymin), np.log10(ymax), ynumticks))
1076+
else:
1077+
ax.set_yticks(np.linspace(ymin, ymax, ynumticks))
1078+
1079+
else:
1080+
# DEFAULT behavior
1081+
if scale_y == "log":
1082+
ax.yaxis.set_major_locator(ticker.LogLocator(base=10))
1083+
ax.yaxis.set_major_formatter(ticker.LogFormatterSciNotation(base=10))
1084+
else:
1085+
ax.yaxis.set_major_locator(ticker.MaxNLocator(nbins=detault_nticks, min_n_ticks=4))
1086+
sf = ticker.ScalarFormatter(useMathText=True)
1087+
sf.set_powerlimits((-3, 4))
1088+
ax.yaxis.set_major_formatter(sf)
1089+
1090+
10171091
# --- case 3: existing figure, adding more data ---
10181092
else:
10191093
loc = getattr(ax, '_legend_loc', 'best')
@@ -1028,72 +1102,6 @@ def plot_to_fig(x_data,y_data,**kwargs):
10281102
fontsize=fontsize,
10291103
framealpha=framealpha)
10301104

1031-
1032-
# plot title
1033-
if kwargs.get('plot_title'):
1034-
if kwargs.get('title_fontsize'):
1035-
title_fontsize=kwargs.get('title_fontsize')
1036-
else:
1037-
title_fontsize=default_title_fontsize
1038-
1039-
plt.text(0.05, 0.95, kwargs.get('plot_title'),
1040-
transform=plt.gca().transAxes,
1041-
fontsize=title_fontsize,
1042-
verticalalignment='top',
1043-
horizontalalignment='left')
1044-
1045-
# set axes and tick properties
1046-
xmin=kwargs.get('x_min')
1047-
xmax=kwargs.get('x_max')
1048-
ymin=kwargs.get('y_min')
1049-
ymax=kwargs.get('y_max')
1050-
1051-
ax.set_xlim(xmin,xmax)
1052-
ax.set_ylim(ymin,ymax)
1053-
1054-
# --- Tick handling AFTER limits are set ---
1055-
if ax.get_xscale() == 'linear':
1056-
# ----- LINEAR AXIS -----
1057-
if xticks is None and xnumticks is None:
1058-
ax.xaxis.set_major_locator(ticker.MaxNLocator(nbins=detault_nticks, min_n_ticks=4))
1059-
sf = ticker.ScalarFormatter(useMathText=True)
1060-
sf.set_powerlimits((-3, 4))
1061-
ax.xaxis.set_major_formatter(sf)
1062-
1063-
elif ax.get_xscale() == 'log':
1064-
# ----- LOG AXIS -----
1065-
# Do not touch scalar formatters – LogFormatter is correct.
1066-
ax.xaxis.set_major_locator(ticker.LogLocator(base=10))
1067-
ax.xaxis.set_major_formatter(ticker.LogFormatterSciNotation(base=10))
1068-
1069-
# Same for y-axis
1070-
if ax.get_yscale() == 'linear':
1071-
if yticks is None and ynumticks is None:
1072-
ax.yaxis.set_major_locator(ticker.MaxNLocator(nbins=detault_nticks, min_n_ticks=4))
1073-
sf = ticker.ScalarFormatter(useMathText=True)
1074-
sf.set_powerlimits((-3, 4))
1075-
ax.yaxis.set_major_formatter(sf)
1076-
1077-
elif ax.get_yscale() == 'log':
1078-
ax.yaxis.set_major_locator(ticker.LogLocator(base=10))
1079-
ax.yaxis.set_major_formatter(ticker.LogFormatterSciNotation(base=10))
1080-
1081-
# set number of ticks if requested by the user
1082-
if xnumticks is not None:
1083-
if plot_type in ('loglog', 'semilogx'):
1084-
ax.set_xticks(np.logspace(xmin, xmax, xnumticks))
1085-
else:
1086-
ax.set_xticks(np.linspace(xmin, xmax, xnumticks))
1087-
if ynumticks is not None:
1088-
if plot_type in ('loglog', 'semilogy'):
1089-
ax.set_yticks(np.logspace(ymin, ymax, ynumticks))
1090-
else:
1091-
ax.set_yticks(np.linspace(ymin, ymax, ynumticks))
1092-
1093-
# set ticks if requested by the user
1094-
if xticks is not None: ax.set_xticks(xticks)
1095-
if yticks is not None: ax.set_yticks(yticks)
1096-
10971105
if kwargs.get('revision_label'):
10981106
add_version_string(ax=ax, version_str=kwargs.get('revision_label'), plot_type=plot_type, font_size=version_fontsize)
10991107

Utilities/Python/scripts/McCaffrey_Plume.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,8 @@
160160
y_label=r'$V/Q^{1/5}$ $({\rm m s}^{-1} \; {\rm kW}^{-1/5})$',
161161
plot_title=f'McCaffrey Centerline Velocity, {res}',
162162
legend_location='lower right',
163-
plot_type='loglog')
163+
plot_type='loglog',
164+
yticks = [0.6, 1, 2, 3])
164165

165166
for i, c in enumerate(chid):
166167
fname = os.path.join(datadir, f'{c}_line.csv')
@@ -175,7 +176,7 @@
175176
v = M.iloc[:, 1].values
176177
zq_fds = z / (Q[i]**(2.0/5.0))
177178
vq_fds = v / (Q[i]**(1.0/5.0))
178-
fdsplotlib.plot_to_fig(x_data=zq_fds, y_data=vq_fds,
179+
fdsplotlib.plot_to_fig(x_data=zq_fds, y_data=vq_fds,
179180
data_label=f'{Q[i]} kW', marker_style=mark[i], figure_handle=fig_v)
180181
fig_v.savefig(os.path.join(pltdir, f'McCaffrey_Velocity_Correlation_{res}.pdf'))
181182
plt.close(fig_v)
@@ -189,7 +190,7 @@
189190
x_label=r'$z/Q^{2/5}$ $({\rm m kW}^{-2/5})$',
190191
y_label=r'$\Delta{T}$ ($^\circ$C)',
191192
plot_title=f'McCaffrey Centerline Temperature, {res}',
192-
legend_location='lower right',
193+
legend_location='lower left',
193194
plot_type='loglog')
194195

195196
for i, c in enumerate(chid):
@@ -203,7 +204,7 @@
203204
z = M.iloc[:, 0].values
204205
T = M.iloc[:, 2].values + 273.15 if M.shape[1] > 2 else M.iloc[:, 1].values + 273.15
205206
zq_fds = z / (Q[i]**(2.0/5.0))
206-
fdsplotlib.plot_to_fig(x_data=zq_fds, y_data=T-T0,
207+
fdsplotlib.plot_to_fig(x_data=zq_fds, y_data=T-T0,
207208
data_label=f'{Q[i]} kW', marker_style=mark[i], figure_handle=fig_t)
208209
fig_t.savefig(os.path.join(pltdir, f'McCaffrey_Temperature_Correlation_{res}.pdf'))
209210
plt.close(fig_t)

0 commit comments

Comments
 (0)