diff --git a/Utilities/Python/fdsplotlib.py b/Utilities/Python/fdsplotlib.py index 72a91de0f6..f81da30de6 100644 --- a/Utilities/Python/fdsplotlib.py +++ b/Utilities/Python/fdsplotlib.py @@ -708,44 +708,39 @@ def get_data(E, spec, start_idx): return y, col_names -def configure_fds_fonts(): +def configure_fds_fonts(**kwargs): import matplotlib.pyplot as plt import platform system = platform.system() + use_tex = kwargs.get('usetex', False) if system == "Linux": - # Linux: use Nimbus Roman as primary serif, with Times ahead of Times New Roman in the fallback chain primary_serif = "Nimbus Roman" serif_list = [ - "Nimbus Roman", # primary on Linux + "Nimbus Roman", "Times", "Times New Roman", "serif", ] else: - # macOS ("Darwin") and Windows: prefer Times, then Times New Roman primary_serif = "Times" serif_list = [ - "Times", # first choice + "Times", "Times New Roman", "Nimbus Roman", "serif", ] plt.rcParams.update({ - # Core-14 fonts for small PDFs (Times-Roman in output) "pdf.use14corefonts": True, - "text.usetex": False, + "text.usetex": use_tex, - # Make serif the default everywhere "font.family": "serif", "font.serif": serif_list, - - # If something explicitly requests 'sans-serif', try to keep it Times-like too "font.sans-serif": serif_list, - # Math text: follow the same primary serif + # Mathtext still matters when text.usetex=False "mathtext.fontset": "custom", "mathtext.rm": primary_serif, "mathtext.it": f"{primary_serif}:italic", @@ -758,6 +753,13 @@ def configure_fds_fonts(): "pdf.compression": 9, }) + # Only for usetex=True, insert packages that Times-ify math + if use_tex: + plt.rcParams["text.latex.preamble"] = r""" + \usepackage{newtxtext} + \usepackage{newtxmath} + """ + def plot_to_fig(x_data,y_data,**kwargs): """ @@ -774,8 +776,6 @@ def plot_to_fig(x_data,y_data,**kwargs): plot_style = get_plot_style("fds") - configure_fds_fonts() - # plt.rcParams.update({ # "pdf.use14corefonts": True, # "text.usetex": False, @@ -828,17 +828,33 @@ def plot_to_fig(x_data,y_data,**kwargs): # if figure handle is passed, append to current figure, else generate a new figure if kwargs.get('figure_handle'): fig = kwargs.get('figure_handle') + if fig.axes: ax = fig.axes[0] else: - # Ensure we have at least one axes ax = fig.add_subplot(111) + plt.figure(fig.number) using_existing_figure = True + + # ---- restore original usetex for this figure ---- + use_tex = getattr(fig, "_fds_usetex", False) + plt.rcParams["text.usetex"] = use_tex + else: fig = plt.figure(figsize=figure_size) using_existing_figure = False - # Convert to fractions of the figure size: + + # Take usetex from kwargs, default to False + use_tex = kwargs.get('usetex', False) + + # ---- apply font settings before any text is drawn ---- + configure_fds_fonts(usetex=use_tex) + + # ---- record the choice on the figure for later calls ---- + fig._fds_usetex = use_tex + + # Create axes ax_w = plot_size[0] / figure_size[0] ax_h = plot_size[1] / figure_size[1] left = plot_origin[0] / figure_size[0] diff --git a/Utilities/Python/scripts/compression_wave.py b/Utilities/Python/scripts/compression_wave.py index 48c32ebed2..46ff4d498d 100644 --- a/Utilities/Python/scripts/compression_wave.py +++ b/Utilities/Python/scripts/compression_wave.py @@ -123,12 +123,13 @@ def clog(real_expr): x_min=0, x_max=12.5, y_min=0, y_max=8, revision_label=version_string, x_label='Time (s)', - y_label='Density (kg/m$^3$)') + y_label=r'Density (kg/m$^3$)', + usetex=True) -fdsplotlib.plot_to_fig(x_data=t_FL4_16, y_data=rho_fds_FL4_16, marker_style='c--', data_label='FDS $N=16$', figure_handle=fig) -fdsplotlib.plot_to_fig(x_data=t_FL4_32, y_data=rho_fds_FL4_32, marker_style='g--', data_label='FDS $N=32$', figure_handle=fig) -fdsplotlib.plot_to_fig(x_data=t_FL4_64, y_data=rho_fds_FL4_64, marker_style='b--', data_label='FDS $N=64$', figure_handle=fig) -fdsplotlib.plot_to_fig(x_data=t_FL4_128,y_data=rho_fds_FL4_128,marker_style='r--', data_label='FDS $N=128$',figure_handle=fig) +fdsplotlib.plot_to_fig(x_data=t_FL4_16, y_data=rho_fds_FL4_16, marker_style='c--', data_label=r'FDS $N=16$', figure_handle=fig) +fdsplotlib.plot_to_fig(x_data=t_FL4_32, y_data=rho_fds_FL4_32, marker_style='g--', data_label=r'FDS $N=32$', figure_handle=fig) +fdsplotlib.plot_to_fig(x_data=t_FL4_64, y_data=rho_fds_FL4_64, marker_style='b--', data_label=r'FDS $N=64$', figure_handle=fig) +fdsplotlib.plot_to_fig(x_data=t_FL4_128,y_data=rho_fds_FL4_128,marker_style='r--', data_label=r'FDS $N=128$',figure_handle=fig) plt.savefig(os.path.join(pltdir, 'compression_wave_time_series.pdf'), format='pdf') plt.close() @@ -140,14 +141,15 @@ def clog(real_expr): e_FL2 = np.array([error_FL2_16, error_FL2_32, error_FL2_64, error_FL2_128]) e_FL4 = np.array([error_FL4_16, error_FL4_32, error_FL4_64, error_FL4_128]) -fig = fdsplotlib.plot_to_fig(x_data=h, y_data=0.1*h, marker_style='k--', data_label=r'$O(\delta x)$', +fig = fdsplotlib.plot_to_fig(x_data=h, y_data=0.1*h, marker_style='k--', data_label=r'${\cal O}(\delta x)$', x_min=1e-2, x_max=1, y_min=1e-4, y_max=1e-1, plot_type='loglog', revision_label=version_string, x_label='Grid Spacing (m)', - y_label='L$_2$ Error (kg/m$^3$)') + y_label=r'L2 Error (kg/m$^3$)', + usetex=True) -fdsplotlib.plot_to_fig(x_data=h, y_data=0.1*h**2, marker_style='k-', data_label=r'$O(\delta x^2)$', figure_handle=fig) +fdsplotlib.plot_to_fig(x_data=h, y_data=0.1*h**2, marker_style='k-', data_label=r'${\cal O}(\delta x^2)$', figure_handle=fig) fdsplotlib.plot_to_fig(x_data=h, y_data=e_FL0, marker_style='b*-', data_label='Central', figure_handle=fig) fdsplotlib.plot_to_fig(x_data=h, y_data=e_FL2, marker_style='ro-', data_label='Superbee', figure_handle=fig) fdsplotlib.plot_to_fig(x_data=h, y_data=e_FL4, marker_style='g^-', data_label='CHARM', figure_handle=fig)