Coverage for pySDC/helpers/plot_helper.py: 94%
33 statements
« prev ^ index » next coverage.py v7.6.9, created at 2024-12-20 14:51 +0000
« prev ^ index » next coverage.py v7.6.9, created at 2024-12-20 14:51 +0000
1import matplotlib as mpl
2import matplotlib.pyplot as plt
3from distutils.spawn import find_executable
5default_mpl_params = mpl.rcParams.copy()
8def figsize(textwidth, scale, ratio):
9 """
10 Get figsize.
12 Args:
13 textwidth (str): Textwdith in your LaTeX file in points
14 scale (float): The width of the figure relative to the textwidth
15 ratio (float): The height of the figure relative to its width
17 Returns:
18 list: Width and height of the figure to be passed to matplotlib
19 """
20 fig_width_pt = textwidth # Get this from LaTeX using \the\textwidth
21 inches_per_pt = 1.0 / 72.27 # Convert pt to inch
22 fig_width = fig_width_pt * inches_per_pt * scale # width in inches
23 fig_height = fig_width * ratio # height in inches
24 fig_size = [fig_width, fig_height]
25 return fig_size
28def figsize_by_journal(journal, scale, ratio): # pragma: no cover
29 """
30 Get figsize for specific journal. If you supply a text height, we will rescale the figure to fit on the page instead
31 of the parameters supplied.
33 Args:
34 journal (str): Name of journal
35 scale (float): The width of the figure relative to the textwidth
36 ratio (float): The height of the figure relative to its width
38 Returns:
39 list: Width and height of the figure to be passed to matplotlib
40 """
41 # store text width in points here, get this from LaTeX using \the\textwidth
42 textwidths = {
43 'JSC_beamer': 426.79135,
44 'Springer_Numerical_Algorithms': 338.58778,
45 'JSC_thesis': 434.26027,
46 'TUHH_thesis': 426.79135,
47 }
48 # store text height in points here, get this from LaTeX using \the\textheight
49 textheights = {
50 'JSC_beamer': 214.43411,
51 'JSC_thesis': 635.5,
52 'TUHH_thesis': 631.65118,
53 }
54 assert (
55 journal in textwidths.keys()
56 ), f"Textwidth only available for {list(textwidths.keys())}. Please implement one for \"{journal}\"! Get the textwidth using \"\\the\\textwidth\" in your tex file."
58 # see if the figure fits on the page or if we need to apply the scaling to the height instead
59 if scale * ratio * textwidths[journal] > textheights.get(journal, 1e9):
60 if textheights[journal] / scale / ratio > textwidths[journal]:
61 raise ValueError(
62 f"We cannot fit figure with scale {scale:.2f} and ratio {ratio:.2f} on the page for journal {journal}!"
63 )
64 return figsize(textheights[journal] / (scale * ratio), 1, ratio)
66 return figsize(textwidths[journal], scale, ratio)
69def setup_mpl(font_size=8, reset=False):
70 if reset:
71 mpl.rcParams.update(default_mpl_params)
73 # Set up plotting parameters
74 style_options = { # setup matplotlib to use latex for output
75 "font.family": "serif",
76 "font.serif": [], # blank entries should cause plots to inherit fonts from the document
77 "font.sans-serif": [],
78 "font.monospace": [],
79 # "axes.labelsize": 8, # LaTeX default is 10pt font.
80 "axes.linewidth": 0.5,
81 "font.size": font_size,
82 # "legend.fontsize": 6, # Make the legend/label fonts a little smaller
83 "legend.numpoints": 1,
84 # "xtick.labelsize": 6,
85 "xtick.major.width": 0.5, # major tick width in points
86 "xtick.minor.width": 0.25,
87 # "ytick.labelsize": 6,
88 "ytick.major.width": 0.5, # major tick width in points
89 "ytick.minor.width": 0.25,
90 "lines.markersize": 4,
91 "lines.markeredgewidth": 0.5,
92 "grid.linewidth": 0.5,
93 "grid.linestyle": '-',
94 "grid.alpha": 0.25,
95 "figure.subplot.hspace": 0.0,
96 "savefig.pad_inches": 0.01,
97 }
99 mpl.rcParams.update(style_options)
101 if find_executable('latex'):
102 latex_support = {
103 "pgf.texsystem": "pdflatex", # change this if using xetex or lautex
104 "text.usetex": True, # use LaTeX to write all text
105 "pgf.preamble": r"\usepackage[utf8x]{inputenc}"
106 r"\usepackage[T1]{fontenc}"
107 r"\usepackage{underscore}"
108 r"\usepackage{amsmath,amssymb,marvosym}",
109 }
110 else:
111 latex_support = {
112 "text.usetex": False, # use LaTeX to write all text
113 }
115 mpl.rcParams.update(latex_support)
116 plt.close('all')
119def newfig(textwidth, scale, ratio=0.6180339887):
120 plt.clf()
121 fig, ax = plt.subplots(figsize=figsize(textwidth, scale, ratio))
122 return fig, ax
125def savefig(filename, save_pdf=True, save_pgf=True, save_png=True):
126 if save_pgf and find_executable('latex'):
127 plt.savefig('{}.pgf'.format(filename), bbox_inches='tight')
128 if save_pdf:
129 plt.savefig('{}.pdf'.format(filename), bbox_inches='tight')
130 if save_png:
131 plt.savefig('{}.png'.format(filename), bbox_inches='tight')
132 plt.close()