Skip to content

Commit 774d968

Browse files
authored
Merge pull request #15666 from rmcdermo/master
Python: add usetex to plot_to_fig call
2 parents c8a7664 + d167700 commit 774d968

File tree

2 files changed

+41
-23
lines changed

2 files changed

+41
-23
lines changed

Utilities/Python/fdsplotlib.py

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -708,44 +708,39 @@ def get_data(E, spec, start_idx):
708708
return y, col_names
709709

710710

711-
def configure_fds_fonts():
711+
def configure_fds_fonts(**kwargs):
712712
import matplotlib.pyplot as plt
713713
import platform
714714

715715
system = platform.system()
716+
use_tex = kwargs.get('usetex', False)
716717

717718
if system == "Linux":
718-
# Linux: use Nimbus Roman as primary serif, with Times ahead of Times New Roman in the fallback chain
719719
primary_serif = "Nimbus Roman"
720720
serif_list = [
721-
"Nimbus Roman", # primary on Linux
721+
"Nimbus Roman",
722722
"Times",
723723
"Times New Roman",
724724
"serif",
725725
]
726726
else:
727-
# macOS ("Darwin") and Windows: prefer Times, then Times New Roman
728727
primary_serif = "Times"
729728
serif_list = [
730-
"Times", # first choice
729+
"Times",
731730
"Times New Roman",
732731
"Nimbus Roman",
733732
"serif",
734733
]
735734

736735
plt.rcParams.update({
737-
# Core-14 fonts for small PDFs (Times-Roman in output)
738736
"pdf.use14corefonts": True,
739-
"text.usetex": False,
737+
"text.usetex": use_tex,
740738

741-
# Make serif the default everywhere
742739
"font.family": "serif",
743740
"font.serif": serif_list,
744-
745-
# If something explicitly requests 'sans-serif', try to keep it Times-like too
746741
"font.sans-serif": serif_list,
747742

748-
# Math text: follow the same primary serif
743+
# Mathtext still matters when text.usetex=False
749744
"mathtext.fontset": "custom",
750745
"mathtext.rm": primary_serif,
751746
"mathtext.it": f"{primary_serif}:italic",
@@ -758,6 +753,13 @@ def configure_fds_fonts():
758753
"pdf.compression": 9,
759754
})
760755

756+
# Only for usetex=True, insert packages that Times-ify math
757+
if use_tex:
758+
plt.rcParams["text.latex.preamble"] = r"""
759+
\usepackage{newtxtext}
760+
\usepackage{newtxmath}
761+
"""
762+
761763

762764
def plot_to_fig(x_data,y_data,**kwargs):
763765
"""
@@ -774,8 +776,6 @@ def plot_to_fig(x_data,y_data,**kwargs):
774776

775777
plot_style = get_plot_style("fds")
776778

777-
configure_fds_fonts()
778-
779779
# plt.rcParams.update({
780780
# "pdf.use14corefonts": True,
781781
# "text.usetex": False,
@@ -828,17 +828,33 @@ def plot_to_fig(x_data,y_data,**kwargs):
828828
# if figure handle is passed, append to current figure, else generate a new figure
829829
if kwargs.get('figure_handle'):
830830
fig = kwargs.get('figure_handle')
831+
831832
if fig.axes:
832833
ax = fig.axes[0]
833834
else:
834-
# Ensure we have at least one axes
835835
ax = fig.add_subplot(111)
836+
836837
plt.figure(fig.number)
837838
using_existing_figure = True
839+
840+
# ---- restore original usetex for this figure ----
841+
use_tex = getattr(fig, "_fds_usetex", False)
842+
plt.rcParams["text.usetex"] = use_tex
843+
838844
else:
839845
fig = plt.figure(figsize=figure_size)
840846
using_existing_figure = False
841-
# Convert to fractions of the figure size:
847+
848+
# Take usetex from kwargs, default to False
849+
use_tex = kwargs.get('usetex', False)
850+
851+
# ---- apply font settings before any text is drawn ----
852+
configure_fds_fonts(usetex=use_tex)
853+
854+
# ---- record the choice on the figure for later calls ----
855+
fig._fds_usetex = use_tex
856+
857+
# Create axes
842858
ax_w = plot_size[0] / figure_size[0]
843859
ax_h = plot_size[1] / figure_size[1]
844860
left = plot_origin[0] / figure_size[0]

Utilities/Python/scripts/compression_wave.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,13 @@ def clog(real_expr):
123123
x_min=0, x_max=12.5, y_min=0, y_max=8,
124124
revision_label=version_string,
125125
x_label='Time (s)',
126-
y_label='Density (kg/m$^3$)')
126+
y_label=r'Density (kg/m$^3$)',
127+
usetex=True)
127128

128-
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)
129-
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)
130-
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)
131-
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)
129+
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)
130+
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)
131+
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)
132+
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)
132133

133134
plt.savefig(os.path.join(pltdir, 'compression_wave_time_series.pdf'), format='pdf')
134135
plt.close()
@@ -140,14 +141,15 @@ def clog(real_expr):
140141
e_FL2 = np.array([error_FL2_16, error_FL2_32, error_FL2_64, error_FL2_128])
141142
e_FL4 = np.array([error_FL4_16, error_FL4_32, error_FL4_64, error_FL4_128])
142143

143-
fig = fdsplotlib.plot_to_fig(x_data=h, y_data=0.1*h, marker_style='k--', data_label=r'$O(\delta x)$',
144+
fig = fdsplotlib.plot_to_fig(x_data=h, y_data=0.1*h, marker_style='k--', data_label=r'${\cal O}(\delta x)$',
144145
x_min=1e-2, x_max=1, y_min=1e-4, y_max=1e-1,
145146
plot_type='loglog',
146147
revision_label=version_string,
147148
x_label='Grid Spacing (m)',
148-
y_label='L$_2$ Error (kg/m$^3$)')
149+
y_label=r'L2 Error (kg/m$^3$)',
150+
usetex=True)
149151

150-
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)
152+
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)
151153
fdsplotlib.plot_to_fig(x_data=h, y_data=e_FL0, marker_style='b*-', data_label='Central', figure_handle=fig)
152154
fdsplotlib.plot_to_fig(x_data=h, y_data=e_FL2, marker_style='ro-', data_label='Superbee', figure_handle=fig)
153155
fdsplotlib.plot_to_fig(x_data=h, y_data=e_FL4, marker_style='g^-', data_label='CHARM', figure_handle=fig)

0 commit comments

Comments
 (0)