plot_style.py 2.79 KB
Newer Older
1
#!/usr/bin/python
Christian Schneider's avatar
Christian Schneider committed
2
3
"""This file contains a normalized plot style for our group."""

4
5
6
7
8
# Try to import matplotlib
try:
    import matplotlib.pyplot as plt
except NotImplementedError:
    pass
9
import builtins
Christian Schneider's avatar
Christian Schneider committed
10
import re
Christian Schneider's avatar
Christian Schneider committed
11
12
13
import matplotlib as mpl
import os

Christian Schneider's avatar
Christian Schneider committed
14

15
# Colorscheme taken from http://colorbrewer2.org/
16
17
18
19
20
21
#color_scheme = ['#377EB8', '#E41A1C', '#4DAF4A', '#984EA3', '#FF7F00',
#                '#FFFF33', '#A65628', '#F781BF', '#000000']
color_scheme = ['#0072bd','#d95319','#edb120','#7e2f8e','#77ac30','#4dbeee',
                '#a2142f', '#000000']
#color_labels = ['b', 'r', 'g', 'p', 'mand', 'y', 'br', 'pink', 'k']
color_labels = ['b', 'mand', 'y', 'p', 'g', 'lb', 'r', 'k']
Christian Schneider's avatar
Christian Schneider committed
22
#color_scheme = Colorblind8
Christian Schneider's avatar
Bugfix    
Christian Schneider committed
23
cc = dict(zip(color_labels, color_scheme))
Christian Schneider's avatar
Christian Schneider committed
24
builtins.cc = cc  # Make this a global variable for easy access
25

Christian Schneider's avatar
Christian Schneider committed
26
27
28
29
30
31
32
33
# UIBK Colormap
with open( os.path.dirname(__file__) + '/styles/uibk_colormap_rgb', 'r') as f:
    a = f.read().splitlines()
    a = [[float(i)/255 for i in k.split(' ')] for k in a]
builtins.cmap_uibk = mpl.colors.ListedColormap(a)


# Default styles
Christian Schneider's avatar
Christian Schneider committed
34
def set(font='Serif', fontsize=11, figsize=(8.6, 8.6),
35
36
        linewidth=1.5, color_scheme=color_scheme,
        color_labels=color_labels):
Christian Schneider's avatar
Christian Schneider committed
37
    """Function to set global plot style.
38

Christian Schneider's avatar
Christian Schneider committed
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
    Parameters
    -----------
    font : str
        Default font. Chose from ['Sans-Serif, 'Serif', 'Times New Roman', ...]
    fontsize : int
        Fontsize. Defaults is 11
    figsize : list
        Figure size in cm. [(x_dim (cm), y_dim (cm))]. For example (8.6, 8.6)]
        for the default PRL single column figure.
    linewidth : float
        Default linewidth
    color_scheme : list
        Colors for plot. Default is:
        [['#377EB8', '#E41A1C', '#4DAF4A', '#984EA3', '#FF7F00', '#FFFF33',
        '#A65628', '#F781BF']
    color_labels : list
        Abbreviations array for colors. Default is:
        ['b', 'r', 'g', 'p', 'mand', 'y', 'br', 'pink']
57
58
59
60
61
62
63
64
65
    """
    params = {
               'font.size': fontsize,
               'backend': 'PDF',
               'font.family': font,
               'figure.figsize': (figsize[0]/2.54, figsize[1]/2.54),
               'axes.prop_cycle': plt.cycler('color', color_scheme),
               'axes.formatter.useoffset': False,
               'lines.linewidth': linewidth,
66
               'axes.axisbelow': True,  # Grid axis below data
67
68
               'grid.color': '#BFBFBF',
               'grid.linestyle': '-',
Christian Schneider's avatar
Christian Schneider committed
69
70
               'legend.fontsize': 10,
               'figure.dpi': 120
71
72
73
74
             }

    plt.rcParams.update(params)
    builtins.cc = dict(zip(color_labels, color_scheme))
75
76


Christian Schneider's avatar
Christian Schneider committed
77
def check_color(style):
Christian Schneider's avatar
Christian Schneider committed
78
    """Help function, to check if color is part of the default colors"""
Christian Schneider's avatar
Christian Schneider committed
79
80
81
82
83
    for kw in list(cc.keys()):
        m = re.search(kw, style)
        if m:
            return m.group()

84
85
    # Return 'b' if nothing has found
    return 'b'