-
Notifications
You must be signed in to change notification settings - Fork 3
/
plotting.py
48 lines (43 loc) · 2.01 KB
/
plotting.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
import numpy as np
import matplotlib as mpl
mpl.use('pgf')
import os
def figsize(scale, nplots = 1):
fig_width_pt = 390.0 # Get this from LaTeX using \the\textwidth
inches_per_pt = 1.0/72.27 # Convert pt to inch
golden_mean = (np.sqrt(5.0)-1.0)/2.0 # Aesthetic ratio (you could change this)
fig_width = fig_width_pt*inches_per_pt*scale # width in inches
fig_height = nplots*fig_width*golden_mean # height in inches
fig_size = [fig_width,fig_height]
return fig_size
pgf_with_latex = { # setup matplotlib to use latex for output
"pgf.texsystem": "pdflatex", # change this if using xetex or lautex
"text.usetex": True, # use LaTeX to write all text
"font.family": "serif",
"font.serif": [], # blank entries should cause plots to inherit fonts from the document
"font.sans-serif": [],
"font.monospace": [],
"axes.labelsize": 10, # LaTeX default is 10pt font.
"font.size": 10,
"legend.fontsize": 8, # Make the legend/label fonts a little smaller
"xtick.labelsize": 8,
"ytick.labelsize": 8,
"figure.figsize": figsize(1.0), # default fig size of 0.9 textwidth
"pgf.preamble": [
r"\usepackage[utf8x]{inputenc}", # use utf8 fonts becasue your computer can handle it :)
r"\usepackage[T1]{fontenc}", # plots will be generated using this preamble
]
}
mpl.rcParams.update(pgf_with_latex)
import matplotlib.pyplot as plt
def newfig(width, nplots = 1):
fig = plt.figure(figsize=figsize(width, nplots))
ax = fig.add_subplot(111)
return fig, ax
def savefig(filename, crop = True):
if crop == True:
plt.savefig('{}.pdf'.format(filename), bbox_inches='tight', pad_inches=0)
plt.savefig('{}.png'.format(filename), bbox_inches='tight', pad_inches=0)
else:
plt.savefig('{}.pdf'.format(filename))
plt.savefig('{}.png'.format(filename))