00001
00002
00003
00004
00005 import os
00006 import math
00007 from colorsys import hls_to_rgb, rgb_to_hls
00008
00009 import matplotlib
00010 from matplotlib.font_manager import FontProperties
00011 from matplotlib._png import read_png
00012 from matplotlib.offsetbox import OffsetImage
00013 from matplotlib.offsetbox import AnnotationBbox
00014
00015 import numpy as np
00016
00017
00018
00019 FONT_PROPS_SUPTITLE = FontProperties(size="x-large", weight="bold", stretch="condensed")
00020 FONT_PROPS_TITLE = FontProperties(size="large", weight="regular")
00021 FONT_PROPS_AX_TITLE = FontProperties(size="x-large", weight="bold")
00022 FONT_PROPS_TICK_LABEL = FontProperties(size="large", weight="bold")
00023
00024
00025
00026 def InitMatplotlib():
00027 """Just some Matplotlib settings."""
00028 matplotlib.rcParams["text.usetex"] = False
00029 matplotlib.rcParams["legend.numpoints"] = 1
00030 matplotlib.rcParams["figure.figsize"] = (8., 6.)
00031 matplotlib.rcParams["figure.dpi"] = 300
00032 matplotlib.rcParams["savefig.dpi"] = matplotlib.rcParams["figure.dpi"]
00033 matplotlib.rcParams["font.size"] = 10.8
00034 matplotlib.rcParams["pdf.fonttype"] = 42
00035
00036
00037
00038
00039 def AddLogo(logo_name, ax, zoom=1.2):
00040 """Read logo from PNG file and add it to axes."""
00041
00042 logo_data = read_png(logo_name)
00043 fig_dpi = ax.get_figure().dpi
00044 fig_size = ax.get_figure().get_size_inches()
00045
00046 zoom_factor = .1 / 1.2 * fig_dpi * fig_size[0] / np.shape(logo_data)[0]
00047 zoom_factor *= zoom
00048 logo_box = OffsetImage(logo_data, zoom=zoom_factor)
00049 ann_box = AnnotationBbox(logo_box, [0., 1.],
00050 xybox=(2., -3.),
00051 xycoords="axes fraction",
00052 boxcoords="offset points",
00053 box_alignment=(0., 1.),
00054 pad=0., frameon=False)
00055 ax.add_artist(ann_box)
00056
00057
00058
00059
00060 def RoundAwayFromZero(val):
00061
00062 res = None
00063 if val < 0.:
00064 res = math.floor(val)
00065 else:
00066 res = math.ceil(val)
00067
00068
00069 return res
00070
00071
00072
00073 def LatexifyUnits(units_in):
00074
00075 latex_units = {
00076 "b^{-1}" : "$\mathbf{b}^{-1}$",
00077 "mb^{-1}" : "$\mathbf{mb}^{-1}$",
00078 "ub^{-1}" : "$\mu\mathbf{b}^{-1}$",
00079 "nb^{-1}" : "$\mathbf{nb}^{-1}$",
00080 "pb^{-1}" : "$\mathbf{pb}^{-1}$",
00081 "fb^{-1}" : "$\mathbf{fb}^{-1}$",
00082 "Hz/b" : "$\mathbf{Hz/b}$",
00083 "Hz/mb" : "$\mathbf{Hz/mb}$",
00084 "Hz/ub" : "$\mathbf{Hz/}\mathbf{\mu}\mathbf{b}$",
00085 "Hz/nb" : "$\mathbf{Hz/nb}$",
00086 "Hz/pb" : "$\mathbf{Hz/pb}$",
00087 "Hz/fb" : "$\mathbf{Hz/fb}$"
00088 }
00089
00090 res = latex_units[units_in]
00091
00092
00093 return res
00094
00095
00096
00097 def DarkenColor(color_in):
00098 """Takes a tuple (r, g, b) as input."""
00099
00100 color_tmp = matplotlib.colors.colorConverter.to_rgb(color_in)
00101
00102 tmp = rgb_to_hls(*color_tmp)
00103 color_out = hls_to_rgb(tmp[0], .7 * tmp[1], tmp[2])
00104
00105
00106 return color_out
00107
00108
00109
00110 class ColorScheme(object):
00111 """A bit of a cludge, but a simple way to store color choices."""
00112
00113 @classmethod
00114 def InitColors(cls):
00115
00116
00117
00118
00119
00120
00121 ColorScheme.cms_blue = (0./255., 152./255., 212./255.)
00122
00123
00124 ColorScheme.cms_orange = (241./255., 194./255., 40./255.)
00125
00126
00127 ColorScheme.cms_blue_dark = (102./255., 153./255., 204./255.)
00128 ColorScheme.cms_orange_dark = (255./255., 153./255., 0./255.)
00129
00130
00131
00132
00133
00134
00135
00136
00137 ColorScheme.cms_red = (208./255., 0./255., 37./255.)
00138 ColorScheme.cms_yellow = (255./255., 248./255., 0./255.)
00139 ColorScheme.cms_purple = (125./255., 16./255., 123./255.)
00140 ColorScheme.cms_green = (60./255., 177./255., 110./255.)
00141 ColorScheme.cms_orange2 = (227./255., 136./255., 36./255.)
00142
00143
00144
00145 def __init__(self, name):
00146
00147 self.name = name
00148
00149
00150 self.color_fill_del = "black"
00151 self.color_fill_rec = "white"
00152 self.color_fill_peak = "black"
00153 self.color_line_del = DarkenColor(self.color_fill_del)
00154 self.color_line_rec = DarkenColor(self.color_fill_rec)
00155 self.color_line_peak = DarkenColor(self.color_fill_peak)
00156 self.color_by_year = {
00157 2010 : "green",
00158 2011 : "red",
00159 2012 : "blue"
00160 }
00161 self.color_line_pileup = "black"
00162 self.color_fill_pileup = "blue"
00163 self.logo_name = "cms_logo.png"
00164 self.file_suffix = "_%s" % self.name.lower()
00165
00166 tmp_name = self.name.lower()
00167 if tmp_name == "greg":
00168
00169 self.color_fill_del = ColorScheme.cms_blue
00170 self.color_fill_rec = ColorScheme.cms_orange
00171 self.color_fill_peak = ColorScheme.cms_orange
00172 self.color_line_del = DarkenColor(self.color_fill_del)
00173 self.color_line_rec = DarkenColor(self.color_fill_rec)
00174 self.color_line_peak = DarkenColor(self.color_fill_peak)
00175 self.color_line_pileup = "black"
00176 self.color_fill_pileup = ColorScheme.cms_blue
00177 self.logo_name = "cms_logo.png"
00178 self.file_suffix = ""
00179 elif tmp_name == "joe":
00180
00181 self.color_fill_del = ColorScheme.cms_yellow
00182 self.color_fill_rec = ColorScheme.cms_red
00183 self.color_fill_peak = ColorScheme.cms_red
00184 self.color_line_del = DarkenColor(self.color_fill_del)
00185 self.color_line_rec = DarkenColor(self.color_fill_rec)
00186 self.color_line_peak = DarkenColor(self.color_fill_peak)
00187 self.color_line_pileup = "black"
00188 self.color_fill_pileup = ColorScheme.cms_yellow
00189 self.logo_name = "cms_logo_alt.png"
00190 self.file_suffix = "_alt"
00191 else:
00192 print >> sys.stderr, \
00193 "ERROR Unknown color scheme '%s'" % self.name
00194 sys.exit(1)
00195
00196
00197
00198 logo_path = os.path.realpath(os.path.dirname(__file__))
00199 self.logo_name = os.path.join(logo_path,
00200 "../plotdata/%s" % self.logo_name)
00201
00202
00203
00204
00205
00206
00207
00208 def SavePlot(fig, file_name_base):
00209 """Little helper to save plots in various formats."""
00210
00211
00212
00213 assert len(fig.axes) == 2
00214 assert len(fig.axes[0].artists) == 1
00215 assert file_name_base.find(".") < 0
00216
00217
00218
00219 fig.savefig("%s.png" % file_name_base)
00220
00221
00222
00223 tmp_annbox = fig.axes[0].artists[0]
00224 tmp_offsetbox = tmp_annbox.offsetbox
00225 fig_dpi = fig.dpi
00226 tmp_offsetbox.set_zoom(tmp_offsetbox.get_zoom() * 72. / fig_dpi)
00227 tmp = tmp_annbox.xytext
00228 tmp_annbox.xytext = (tmp[0] + 1., tmp[1] - 1.)
00229 fig.savefig("%s.pdf" % file_name_base, dpi=600)
00230
00231
00232
00233