-
Notifications
You must be signed in to change notification settings - Fork 1
/
plotting_tools.py
71 lines (62 loc) · 1.99 KB
/
plotting_tools.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
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib as mpl
### This part enables plotting pdf files ###
plt.switch_backend("Agg")
mpl.rcParams['lines.markersize'] = 3
nice_fonts = {
# Use LaTeX to write all text
"text.usetex": True,
"font.family": "serif",
# Use 10pt font in plots, to match 10pt font in document
"axes.labelsize": 10,
"font.size": 10,
# Make the legend/label fonts a little smaller
"legend.fontsize": 8,
"xtick.labelsize": 7,
"ytick.labelsize": 7,
}
mpl.rcParams.update(nice_fonts)
sns.set_style()
np.set_printoptions(formatter={'float': "{:0.3f}".format})
### End ###
def set_size(width, fraction=1, subplots=(1, 1)):
# https://jwalton.info/Embed-Publication-Matplotlib-Latex/
"""
Sets figure dimensions to avoid scaling in LaTeX.
Parameters
----------
width: float or string
Document width in points, or string of predined document type
fraction: float, optional
Fraction of the width which you wish the figure to occupy
subplots: array-like, optional
The number of rows and columns of subplots.
Returns
-------
fig_dim: tuple
Dimensions of figure in inches
"""
if width == 'thesis':
width_pt = 426.79135
elif width == 'beamer':
width_pt = 307.28987
elif width == 'rpe':
width_pt = 397.48499
elif width == 'neurips22':
width_pt = 397.48499
else:
width_pt = width
# Width of figure (in pts)
fig_width_pt = width_pt * fraction
# Convert from pt to inches
inches_per_pt = 1 / 72.27
# Golden ratio to set aesthetic figure height
# https://disq.us/p/2940ij3
golden_ratio = (5**.5 - 1) / 2
# Figure width in inches
fig_width_in = fig_width_pt * inches_per_pt
# Figure height in inches
fig_height_in = fig_width_in * golden_ratio * (subplots[0] / subplots[1])
return (fig_width_in, fig_height_in)