CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
public_plots_tools.py
Go to the documentation of this file.
1 ######################################################################
2 ## File: public_plots_tools.py
3 ######################################################################
4 
5 import os
6 import math
7 from colorsys import hls_to_rgb, rgb_to_hls
8 
9 import matplotlib
10 from matplotlib.font_manager import FontProperties
11 from matplotlib._png import read_png
12 from matplotlib.offsetbox import OffsetImage
13 from matplotlib.offsetbox import AnnotationBbox
14 
15 import numpy as np
16 
17 ######################################################################
18 
19 FONT_PROPS_SUPTITLE = FontProperties(size="x-large", weight="bold", stretch="condensed")
20 FONT_PROPS_TITLE = FontProperties(size="large", weight="regular")
21 FONT_PROPS_AX_TITLE = FontProperties(size="x-large", weight="bold")
22 FONT_PROPS_TICK_LABEL = FontProperties(size="large", weight="bold")
23 
24 ######################################################################
25 
27  """Just some Matplotlib settings."""
28  matplotlib.rcParams["text.usetex"] = False
29  matplotlib.rcParams["legend.numpoints"] = 1
30  matplotlib.rcParams["figure.figsize"] = (8., 6.)
31  matplotlib.rcParams["figure.dpi"] = 300
32  matplotlib.rcParams["savefig.dpi"] = matplotlib.rcParams["figure.dpi"]
33  matplotlib.rcParams["font.size"] = 10.8
34  matplotlib.rcParams["pdf.fonttype"] = 42
35  # End of InitMatplotlib().
36 
37 ######################################################################
38 
39 def AddLogo(logo_name, ax, zoom=1.2):
40  """Read logo from PNG file and add it to axes."""
41 
42  logo_data = read_png(logo_name)
43  fig_dpi = ax.get_figure().dpi
44  fig_size = ax.get_figure().get_size_inches()
45  # NOTE: This scaling is kinda ad hoc...
46  zoom_factor = .1 / 1.2 * fig_dpi * fig_size[0] / np.shape(logo_data)[0]
47  zoom_factor *= zoom
48  logo_box = OffsetImage(logo_data, zoom=zoom_factor)
49  ann_box = AnnotationBbox(logo_box, [0., 1.],
50  xybox=(2., -3.),
51  xycoords="axes fraction",
52  boxcoords="offset points",
53  box_alignment=(0., 1.),
54  pad=0., frameon=False)
55  ax.add_artist(ann_box)
56  # End of AddLogo().
57 
58 ######################################################################
59 
61 
62  res = None
63  if val < 0.:
64  res = math.floor(val)
65  else:
66  res = math.ceil(val)
67 
68  # End of RoundAwayFromZero().
69  return res
70 
71 ######################################################################
72 
73 def LatexifyUnits(units_in):
74 
75  latex_units = {
76  "b^{-1}" : "$\mathbf{b}^{-1}$",
77  "mb^{-1}" : "$\mathbf{mb}^{-1}$",
78  "ub^{-1}" : "$\mu\mathbf{b}^{-1}$",
79  "nb^{-1}" : "$\mathbf{nb}^{-1}$",
80  "pb^{-1}" : "$\mathbf{pb}^{-1}$",
81  "fb^{-1}" : "$\mathbf{fb}^{-1}$",
82  "Hz/b" : "$\mathbf{Hz/b}$",
83  "Hz/mb" : "$\mathbf{Hz/mb}$",
84  "Hz/ub" : "$\mathbf{Hz/}\mathbf{\mu}\mathbf{b}$",
85  "Hz/nb" : "$\mathbf{Hz/nb}$",
86  "Hz/pb" : "$\mathbf{Hz/pb}$",
87  "Hz/fb" : "$\mathbf{Hz/fb}$"
88  }
89 
90  res = latex_units[units_in]
91 
92  # End of LatexifyUnits().
93  return res
94 
95 ######################################################################
96 
97 def DarkenColor(color_in):
98  """Takes a tuple (r, g, b) as input."""
99 
100  color_tmp = matplotlib.colors.colorConverter.to_rgb(color_in)
101 
102  tmp = rgb_to_hls(*color_tmp)
103  color_out = hls_to_rgb(tmp[0], .7 * tmp[1], tmp[2])
104 
105  # End of DarkenColor().
106  return color_out
107 
108 ######################################################################
109 
111  """A bit of a cludge, but a simple way to store color choices."""
112 
113  @classmethod
114  def InitColors(cls):
115 
116  #------------------------------
117  # For color scheme 'Greg'.
118  #------------------------------
119 
120  # This is the light blue of the CMS logo.
121  ColorScheme.cms_blue = (0./255., 152./255., 212./255.)
122 
123  # This is the orange from the CMS logo.
124  ColorScheme.cms_orange = (241./255., 194./255., 40./255.)
125 
126  # Slightly darker versions of the above colors for the lines.
127  ColorScheme.cms_blue_dark = (102./255., 153./255., 204./255.)
128  ColorScheme.cms_orange_dark = (255./255., 153./255., 0./255.)
129 
130  #------------------------------
131  # For color scheme 'Joe'.
132  #------------------------------
133 
134  # Several colors from the alternative CMS logo, with their
135  # darker line variants.
136 
137  ColorScheme.cms_red = (208./255., 0./255., 37./255.)
138  ColorScheme.cms_yellow = (255./255., 248./255., 0./255.)
139  ColorScheme.cms_purple = (125./255., 16./255., 123./255.)
140  ColorScheme.cms_green = (60./255., 177./255., 110./255.)
141  ColorScheme.cms_orange2 = (227./255., 136./255., 36./255.)
142 
143  # End of InitColors().
144 
145  def __init__(self, name):
146 
147  self.name = name
148 
149  # Some defaults.
150  self.color_fill_del = "black"
151  self.color_fill_rec = "white"
152  self.color_fill_peak = "black"
156  self.color_by_year = {
157  2010 : "green",
158  2011 : "red",
159  2012 : "blue"
160  }
161  self.color_line_pileup = "black"
162  self.color_fill_pileup = "blue"
163  self.logo_name = "cms_logo.png"
164  self.file_suffix = "_%s" % self.name.lower()
165 
166  tmp_name = self.name.lower()
167  if tmp_name == "greg":
168  # Color scheme 'Greg'.
169  self.color_fill_del = ColorScheme.cms_blue
170  self.color_fill_rec = ColorScheme.cms_orange
171  self.color_fill_peak = ColorScheme.cms_orange
175  self.color_line_pileup = "black"
176  self.color_fill_pileup = ColorScheme.cms_blue
177  self.logo_name = "cms_logo.png"
178  self.file_suffix = ""
179  elif tmp_name == "joe":
180  # Color scheme 'Joe'.
181  self.color_fill_del = ColorScheme.cms_yellow
182  self.color_fill_rec = ColorScheme.cms_red
183  self.color_fill_peak = ColorScheme.cms_red
187  self.color_line_pileup = "black"
188  self.color_fill_pileup = ColorScheme.cms_yellow
189  self.logo_name = "cms_logo_alt.png"
190  self.file_suffix = "_alt"
191  else:
192  print >> sys.stderr, \
193  "ERROR Unknown color scheme '%s'" % self.name
194  sys.exit(1)
195 
196  # Find the full path to the logo PNG file.
197  # NOTE: This is a little fragile, I think.
198  logo_path = os.path.realpath(os.path.dirname(__file__))
199  self.logo_name = os.path.join(logo_path,
200  "../plotdata/%s" % self.logo_name)
201 
202  # End of __init__().
203 
204  # End of class ColorScheme.
205 
206 ######################################################################
207 
208 def SavePlot(fig, file_name_base):
209  """Little helper to save plots in various formats."""
210 
211  # DEBUG DEBUG DEBUG
212  # Check some assumptions.
213  assert len(fig.axes) == 2
214  assert len(fig.axes[0].artists) == 1
215  assert file_name_base.find(".") < 0
216  # DEBUG DEBUG DEBUG end
217 
218  # First save as PNG.
219  fig.savefig("%s.png" % file_name_base)
220 
221  # Then rescale and reposition the logo (which is assumed to be the
222  # only artist in the first pair of axes) and save as PDF.
223  tmp_annbox = fig.axes[0].artists[0]
224  tmp_offsetbox = tmp_annbox.offsetbox
225  fig_dpi = fig.dpi
226  tmp_offsetbox.set_zoom(tmp_offsetbox.get_zoom() * 72. / fig_dpi)
227  tmp = tmp_annbox.xytext
228  tmp_annbox.xytext = (tmp[0] + 1., tmp[1] - 1.)
229  fig.savefig("%s.pdf" % file_name_base, dpi=600)
230 
231  # End of SavePlot().
232 
233 ######################################################################
list object
Definition: dbtoconf.py:77