-
Notifications
You must be signed in to change notification settings - Fork 48
/
latexify.py
73 lines (56 loc) · 2.47 KB
/
latexify.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#Original Author: Prof. Nipun Batra
# nipunbatra.github.io
from math import sqrt
import matplotlib
SPINE_COLOR = 'gray'
def latexify(fig_width=None, fig_height=None, font_size=12, columns=2):
"""Set up matplotlib's RC params for LaTeX plotting.
Call this before plotting a figure.
Parameters
----------
fig_width : float, optional, inches
fig_height : float, optional, inches
columns : {1, 2}
"""
# code adapted from https://scipy.github.io/old-wiki/pages/Cookbook/Matplotlib/LaTeX_Examples.html
# Width and max height in inches for IEEE journals taken from
# computer.org/cms/Computer.org/Journal%20templates/transactions_art_guide.pdf
assert(columns in [1,2])
if fig_width is None:
fig_width = 3.39 if columns==1 else 6.9 # width in inches
if fig_height is None:
golden_mean = (sqrt(5)-1.0)/2.0 # Aesthetic ratio
fig_height = fig_width*golden_mean # height in inches
MAX_HEIGHT_INCHES = 8.0
if fig_height > MAX_HEIGHT_INCHES:
print("WARNING: fig_height too large:" + fig_height +
"so will reduce to" + MAX_HEIGHT_INCHES + "inches.")
fig_height = MAX_HEIGHT_INCHES
# NB (bart): default font-size in latex is 11. This should exactly match
# the font size in the text if the figwidth is set appropriately.
# Note that this does not hold if you put two figures next to each other using
# minipage. You need to use subplots.
params = {'backend': 'ps',
'text.latex.preamble': ['\\usepackage{gensymb}'],
'axes.labelsize': font_size, # fontsize for x and y labels (was 12 and before 10)
'axes.titlesize': font_size,
'font.size': font_size, # was 12 and before 10
'legend.fontsize': font_size, # was 12 and before 10
'xtick.labelsize': font_size,
'ytick.labelsize': font_size,
'text.usetex': True,
'figure.figsize': [fig_width,fig_height],
'font.family': 'serif'
}
matplotlib.rcParams.update(params)
def format_axes(ax):
for spine in ['top', 'right']:
ax.spines[spine].set_visible(False)
for spine in ['left', 'bottom']:
ax.spines[spine].set_color(SPINE_COLOR)
ax.spines[spine].set_linewidth(0.5)
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
for axis in [ax.xaxis, ax.yaxis]:
axis.set_tick_params(direction='out', color=SPINE_COLOR)
return ax