plotting.py 3.03 KB
Newer Older
Daniel Brown's avatar
Daniel Brown committed
1
2
3
4
5
6
# -*- coding: utf-8 -*-
"""
Created on Sat Feb 02 10:35:04 2013

@author: Daniel
"""
7
8
9
10
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
Daniel Brown's avatar
Daniel Brown committed
11

12
13
14
# Should be either display (showing in windows) or paper (saving for paper/report/etc.)
__mode__ = None
__DPI__ = None
Daniel Brown's avatar
Daniel Brown committed
15

16
17
18
19
20
21
22
23
24
25
def in_ipython():
    try:
        cfg = get_ipython() 
        return True
    except NameError:
        return False
        
        
def init_pykat_plotting(mode="display", dpi=100):
    import matplotlib as mpl
Daniel Brown's avatar
Daniel Brown committed
26
    
27
    __DPI__ = int(dpi)
Daniel Brown's avatar
Daniel Brown committed
28
    
29
    if in_ipython():
30
31
32
33
34
35
36
        try:
            from IPython.display import set_matplotlib_formats
            set_matplotlib_formats('pdf', 'svg')
            ipy = get_ipython()
            ipy.magic("matplotlib inline")
        except:
            pass
Daniel Brown's avatar
Daniel Brown committed
37
    
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
    if mode == "display":
        __mode__ = mode
        
    elif mode == "paper":
        __mode__ = mode
        
        mpl.use("pgf")

        pgf_with_pdflatex = {
            "pgf.texsystem": "pdflatex",
            "pgf.preamble": [
                 r"\usepackage{amsmath, amssymb}",
                 r"\usepackage{mathtools, siunitx}" ,
                 r"\usepackage{amsmath}",
                 r"\usepackage[utf8x]{inputenc}",
                 r"\usepackage[T1]{fontenc}"
                 ]
        }
Daniel Brown's avatar
Daniel Brown committed
56

57
58
59
        mpl.rcParams.update(pgf_with_pdflatex)
    else:
        raise(BaseException("Plotting mode must be either 'display' or 'paper'."))
Daniel Brown's avatar
Daniel Brown committed
60
        
Daniel Brown's avatar
Daniel Brown committed
61
62
    mpl.rcParams['axes.color_cycle'] = ['b', 'r', 'k', 'g', 'c', 'm', 'y']
    mpl.rcParams['lines.linewidth'] = 1.2
63
64
65
66
67
68
69
70
71
    mpl.rcParams.update({"figure.figsize": (6, 3.708)})
    mpl.rcParams.update({'font.size': 11})
    mpl.rcParams.update({'figure.dpi': __DPI__})
    mpl.rcParams.update({'savefig.dpi': __DPI__})
    mpl.rcParams.update({'font.family': "serif"})
    mpl.rcParams.update({'axes.grid': True})
    mpl.rcParams.update({'axes.axisbelow': True})
    mpl.rcParams.update({'grid.linewidth': 0.25})
    mpl.rcParams.update({'grid.linestyle': ":"})
Daniel Brown's avatar
Daniel Brown committed
72
    mpl.rcParams.update({'grid.color': (0.6,0.6,0.6,1)})
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
    mpl.rcParams.update({'savefig.bbox': "tight"})
    mpl.rcParams.update({'savefig.pad_inches': 0.05})
    mpl.rcParams.update({'xtick.labelsize': "small"})
    mpl.rcParams.update({'ytick.labelsize': "small"})
    mpl.rcParams.update({'axes.formatter.useoffset': False})

def figure(width="full", height=0.618, textwidth=6, **kwargs):
    """
    Options:
        width: 'full', 'half' (0.49*textwidth) (default: full)
        height: relative height to width (default: 1/golden ratio = 0.618)
        textwidth: Width of text in inches (default: 9.84252 in = 25 cm )
    """
    import matplotlib.pyplot as plt
    
    if width == "full":
        fig_size = [textwidth, textwidth*height]
    elif width == "half":
        fig_size = [textwidth*0.49, textwidth*height*0.49]
Daniel Brown's avatar
Daniel Brown committed
92
    else:
93
        raise(BaseException("width must be either 'full' or 'half'."))
Daniel Brown's avatar
Daniel Brown committed
94
        
95
96
97
98
    fig = plt.figure(figsize=fig_size, dpi=__DPI__)
    
    return fig
    
Daniel Brown's avatar
Daniel Brown committed
99
    
100
    
101